开发者

@RequestParam validation

开发者 https://www.devze.com 2023-04-04 15:26 出处:网络
Is there a way to validate request parameter with spring without checking it in every function? For example, every method in a controller produces list of elements and each method accept \'from\' and

Is there a way to validate request parameter with spring without checking it in every function? 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

I want to configure spring to return BAD_REQUEST or some other status just like it does when it cann开发者_开发问答ot convert string to int parameter.

Thanks.


If you use form-backing objects using @RequestBody, then you can use JSR-303 bean validation, where you annotate your form bean. See http://static.springsource.org/spring/docs/current/spring-framework-reference/html/validation.html#validation-beanvalidation for full details. Then you don't need to bother with Validator objects or other coding - you just annotate.


Directly - no. But if you wrap them in a class, you can have a Validator that .supports(YourClass.class) and validate(..) it.

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

精彩评论

暂无评论...
验证码 换一张
取 消