[英]How to validate two or more beans in a Spring Controller method with Hibernate Validator (JSR 303)
[英]How to validate Controller method argument in Spring with JSR validations?
我正在使用给定的代码验证我的表单字段。
//controller method
public String addBusiness(@Valid @ModelAttribute("myForm") MyForm myForm, ...)
{
//logic will go here.
}
//form
@Component
public class MyForm{
@Pattern(regexp = "[0-9]{3,10}", message = "should be valid number")
public String getZip_code()
{
return this.zip_code;
}
}
现在,我想在其他控制器方法中对zip_code
进行相同的验证,
@RequestMapping(value = "${validation.url}", method = RequestMethod.GET)
@ResponseBody
public List<String> getCityList(@RequestParam(value = "zip_code", required = true) final String zip_code)
{
//logic goes here
}
这怎么可能?
不是。 @Valid
不适用于@RequestParam
注释的参数。 您可以创建一个自定义HandlerMethodArgumentResolver
来执行此操作,也可以自己在方法中进行验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.