尝试简单地验证我的bean的字段而不是手动执行它想要查看Spring Validation,但是到现在为止还没有多少运气。
简而言之:
当我调用@Valid
@RestController
注释的验证
我的代码:
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;
}
}
Te BindingResult
结果对象始终显示零错误,并且永远不会调用validate
,supports
或initBinder
方法。
找到这个tutorial,内容如下:
何时调用@InitBinder方法?
@InitBinder注释方法 如果我们没有指定“&#39;值”,则会在每个HTTP请求上调用 这个注释的元素。
WebDataBinder参数特定于模型属性。这意味着 每次Spring创建一个模型属性时,这个方法都会得到 使用WebDataBinder的新实例调用。
所以我尝试将我的控制器方法更改为添加@ModelAttribute
,并且现在验证代码被调用但是requestBody对象(&#34; utente&#34;对象)是空的,因此验证总是失败,因为字段都是空的:
@ResponseBody
@RequestMapping(value="", method=RequestMethod.PUT)
public <T> ResponseEntity<T> aggiornaUtente(@RequestBody @Valid @ModelAttribute("utente") UtenteLite utente, BindingResult result)
{
...
}
传递utente
方法参数,并以JSON作为请求的主体。
答案 0 :(得分:2)
好的,
经过几次尝试后,我成功地通过在我的pom.xml 中添加了hibernate-validation工件参考来生成一个有用的解决方案。
我错误地认为只有在bean属性上使用usig验证注释(如@NotNull
,@ Pattern等)时,hibernate-validator才是强制性的。)
因此,只有通过添加此片段,我才能解决我的问题(希望这可以节省几个小时的工作给其他人):
<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;
}
}