@RequestParam validation
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
}
精彩评论