4
votes

JSR 303 @Valid annotation can be used to validate input objects to a controller method, as demonstrated on Mkyong.com

Is it possible to use @Valid annotation to validate primitive types like int and long[]? If so, how?

Here is an example Spring MVC method signature that needs to be validated that the parameters are above 0:

@RequestMapping(value = { "/delete" }, method = RequestMethod.POST)
ModelAndView deleteBulk(@RequestParam("userId") int userId, @RequestParam("ids") long[] ids) {
2
long[] is not a primitive typeSJuan76
Why don't you try it? Look at the javadoc of the annotation you want to use.Sotirios Delimanolis

2 Answers

3
votes

No, it can't be done. It's for bean validation, as described here.

1
votes

you can validate @RequestParam/@PathVariable simple objects and primitives, if put @Validated on full controller.

And if you don't use spring-boot, you should declare MethodValidationPostProcessor by yourself

@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
    return new MethodValidationPostProcessor();
}