spring valid request body custom message

Solutions on MaxInterview for spring valid request body custom message by the best coders in the world

showing results for - "spring valid request body custom message"
Corbin
23 Jul 2017
1@ControllerAdvice
2@RestController
3public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
4
5        @Override
6        protected ResponseEntity<Object> handleMethodArgumentNotValid(
7                MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
8
9           // ex.getBindingResult(): extract the bind result for default message. 
10              String errorResult = ex.getBindingResult().toString();
11             CustomizedExceptionHandlerResponse exceptionResponse = new CustomizedExceptionHandlerResponse(
12                    errorResult, new Date(), request.getDescription(false));
13
14            return new ResponseEntity<>(exceptionResponse, HttpStatus.BAD_REQUEST);
15        }
16
17
18}
19
20class CustomizedExceptionHandlerResponse {
21
22   private String message;
23   private String status;
24   private Date timestamp;
25
26   // constuctor, setters, getters...
27}