简体   繁体   English

@RequestParam验证

[英]@RequestParam validation

Is there a way to validate request parameter with spring without checking it in every function? 有没有办法用spring验证请求参数而不在每个函数中检查它? For example, every method in a controller produces list of elements and each method accept 'from' and 'to' parameters, so the validation is: from >=0 && to > 0 && from < to 例如,控制器中的每个方法都会生成元素列表,每个方法都接受'from'和'to'参数,因此验证是: from >=0 && to > 0 && from < to

I want to configure spring to return BAD_REQUEST or some other status just like it does when it cannot convert string to int parameter. 我想配置spring来返回BAD_REQUEST或其他一些状态,就像它无法将字符串转换为int参数一样。

Thanks. 谢谢。

If you use form-backing objects using @RequestBody , then you can use JSR-303 bean validation, where you annotate your form bean. 如果您使用@RequestBody使用表单支持对象,那么您可以使用JSR-303 bean验证,您可以在其中注释表单bean。 See http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#validation-beanvalidation for full details. 有关完整详细信息,请参阅http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#validation-beanvalidation Then you don't need to bother with Validator objects or other coding - you just annotate. 然后你不需要打扰Validator对象或其他编码 - 你只是注释。

Directly - no. 直接 - 没有。 But if you wrap them in a class, you can have a Validator that .supports(YourClass.class) and validate(..) it. 但是如果你将它们包装在一个类中,你可以使用一个Validator来支持.supports(YourClass.class)validate(..)validate(..)

public class Range {
   private int from;
   private int to;
   //setters & getters
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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