Late answer. I recently ran into this problem and found a solution. You can do this as follows: First register the bean from MethodValidationPostProcessor :
@Bean public MethodValidationPostProcessor methodValidationPostProcessor() { return new MethodValidationPostProcessor(); }
and then add @Validated to the level type of your controller:
@RestController @Validated public class FooController { @RequestMapping("/email") public Map<String, Object> validate(@Email(message="θ―·θΎε
₯εζ³ηemailε°ε") @RequestParam String email){ Map<String, Object> result = new HashMap<String, Object>(); result.put("email", email); return result; } }
And if the user requested with an invalid email address, a ConstraintViolationException will be selected. And you can catch it:
@ControllerAdvice public class AmazonExceptionHandler { @ExceptionHandler(ConstraintViolationException.class) @ResponseBody @ResponseStatus(HttpStatus.BAD_REQUEST) public String handleValidationException(ConstraintViolationException e){ for(ConstraintViolation<?> s:e.getConstraintViolations()){ return s.getInvalidValue()+": "+s.getMessage(); } return "θ―·ζ±εζ°δΈεζ³"; }
}
You can check my demo here.
source share