You need to create a bean in the Spring configuration:
@Bean public MethodValidationPostProcessor methodValidationPostProcessor() { return new MethodValidationPostProcessor(); }
You must leave the @Validated annotation on your controller.
And you need an Exception handler in your MyController class to handle a ConstraintViolationException :
@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(); }
After these changes, you will see your message when the check is verified.
PS: I just tried this with your @Size check.
source share