繁体   English   中英

如何使用JSR验证在Spring中验证Controller方法参数?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM