javax validation not working on method parameters.. This is a test code and none of javax validation works on method parameter...
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, params = "action=testAction")
public Test update(
@Size(min = 1) @RequestBody List<String> ids,
@Min(3) @PathVariable String name) {
return doSomething(ids, name);
}
But i have class level validations which works perfectly...
@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public RoleType create (@RequestBody @Validated(FieldType.class) User user) {
...
}
And
@Size(min = 2, max = 10, groups = { FieldType.class }, message = "Invalid user code")
public String getId() {
return _id ;
}
-- Solution --
all steps followed as per the accepted answer. And another addition is annoation on class level
@Validated
class UserController
{
@RequestMapping(value = "/{id}", method = RequestMethod.PUT, params ="action=testAction")
public Test update(@Size(min = 1) @RequestBody List<String> ids,@Min(3) @PathVariable String name) {
return doSomething(ids, name);
}
}
validation will validate the nested object for constraints with the help of javax. validation implementation provider, for example, hibernate validator. @Valid also works for collection-typed fields. In the case of collection-typed fields, each collection element will be validated.
Validating a PathVariable Just as with @RequestParam, we can use any annotation from the javax. validation. constraints package to validate a @PathVariable. The default message can be easily overwritten by setting the message parameter in the @Size annotation.
The @Validated annotation is a class-level annotation that we can use to tell Spring to validate parameters that are passed into a method of the annotated class. We'll learn more about how to use it in the section about validating path variables and request parameters.
you need to register MethodValidationPostProcessor bean to kick method level validation annotation
delegates to a JSR-303 provider for performing method-level validation on annotated methods.
@Bean
public MethodValidationPostProcessor methodValidationPostProcessor() {
return new MethodValidationPostProcessor();
}
then,
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public Test update(
@Size(min = 1) @RequestBody List<String> ids,
@Min(3) @PathVariable("id") String name) {
return doSomething(ids, name);
}
if you want to handle validation exception
@ExceptionHandler(value = { ConstraintViolationException.class })
@ResponseStatus(value = HttpStatus.BAD_REQUEST)
public String handleResourceNotFoundException(ConstraintViolationException e) {
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
StringBuilder strBuilder = new StringBuilder();
for (ConstraintViolation<?> violation : violations ) {
strBuilder.append(violation.getMessage() + "\n");
}
return strBuilder.toString();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With