Spring-从不调用带有@Valid的Validation



试图简单地验证我的bean的一个字段,而不是手动验证,希望检查Spring Validation,但到目前为止运气不好。

简而言之:

当我调用@RestController的方法时,似乎永远不会调用带有@Valid注释的验证

我的代码:

pom.xml(用于验证部分)

<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.1.0.Final</version>
</dependency>

Spring是4.1.1版

验证器

package mypackage;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
public class UtenteValidator implements Validator{
public UtenteValidator() {
// TODO Auto-generated constructor stub
}
@Override
public boolean supports(Class<?> clazz) {
return UtenteLite.class.equals(clazz);
}
//validation test
@Override
public void validate(Object target, Errors errors) {
UtenteLite user = (UtenteLite) target;
if(user.getName != "foo") {
errors.rejectValue("name", "name not correct");
}
}


}

控制器

package myPackage;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UsersController {

public UsersController() {
}
//tried to put @InitBinder, but no luck
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new UtenteValidator());
}
@ResponseBody
@RequestMapping(value="", method=RequestMethod.PUT)
public <T> ResponseEntity<T> aggiornaUtente(@RequestBody @Valid UtenteLite utente, BindingResult result)
{
ResponseEntity<T> responseEntity=null;
return responseEntity;
}

}

BindingResult结果对象始终显示零错误,并且从不调用validatesupportsinitBinder方法。

找到了这个教程,上面写着:

何时调用@InitBinder方法

@InitBinder注释的方法如果我们不指定"value",将在每个HTTP请求上调用元素。

WebDataBinder参数特定于模型属性。这意味着每次Spring创建模型属性时,该方法都会得到使用WebDataBinder的新实例调用。

因此,我尝试将控制器方法更改为添加@ModelAttribute,现在调用验证代码,但requestBody对象("utente"对象)为空,因此验证总是失败,因为字段都为空:

@ResponseBody
@RequestMapping(value="", method=RequestMethod.PUT)
public <T> ResponseEntity<T> aggiornaUtente(@RequestBody @Valid @ModelAttribute("utente") UtenteLite utente, BindingResult result)
{ 
...
}

utente方法参数通过JSON作为请求的主体进行传递。

好的,

经过几次尝试,我只在pom.xml.中添加了hibernate验证工件引用,就成功地生成了一个有效的解决方案

我错误地认为,只有当我在beans属性(如@NotNull、@Pattern等)上使用验证注释时,hibernate验证器才是强制性的

因此,只有添加这个片段,我才能解决我的问题(希望这能为其他人节省几个小时的工作):

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>

现在完整的代码是:

验证器

package mypackage;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
public class UtenteValidator implements Validator{
public UtenteValidator() {
// TODO Auto-generated constructor stub
}
@Override
public boolean supports(Class<?> clazz) {
return UtenteLite.class.equals(clazz);
}
//validation test
@Override
public void validate(Object target, Errors errors) {
UtenteLite user = (UtenteLite) target;
if(user.getName != "foo") {
errors.rejectValue("name", "name not correct");
}
}


}

控制器

package myPackage;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/users")
public class UsersController {

public UsersController() {
}
//tried to put @InitBinder, but no luck
@InitBinder
protected void initBinder(WebDataBinder binder) {
binder.setValidator(new UtenteValidator());
}
@ResponseBody
@RequestMapping(value="", method=RequestMethod.PUT)
public <T> ResponseEntity<T> aggiornaUtente(@RequestBody @Valid UtenteLite utente)
{
ResponseEntity<T> responseEntity=null;
return responseEntity;
}

}

相关内容

  • 没有找到相关文章

最新更新