Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

spring valid request body custom message

@ControllerAdvice
@RestController
public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

        @Override
        protected ResponseEntity<Object> handleMethodArgumentNotValid(
                MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {

           // ex.getBindingResult(): extract the bind result for default message. 
              String errorResult = ex.getBindingResult().toString();
             CustomizedExceptionHandlerResponse exceptionResponse = new CustomizedExceptionHandlerResponse(
                    errorResult, new Date(), request.getDescription(false));

            return new ResponseEntity<>(exceptionResponse, HttpStatus.BAD_REQUEST);
        }


}

class CustomizedExceptionHandlerResponse {

   private String message;
   private String status;
   private Date timestamp;

   // constuctor, setters, getters...
}
Comment

PREVIOUS NEXT
Code Example
Java :: java stringbuilder setcharat 
Java :: java bigdecimal 
Java :: method java 
Java :: boolean in java 
Java :: android iterate through radio group java 
Java :: Java path to file 
Java :: java polymorphism 
Java :: java sort method 
Java :: java anonymous class 
Java :: arrays.sum 
Java :: java implement interface 
Java :: Java How to use Map? 
Java :: combinations in java 
Java :: how to do annotation configuration in spring 
Java :: how to create char type with jpa 
Java :: app "restart" the home activity (and dismiss all other activities). 
Java :: gui open event spigot 
Java :: java add a list to a list 
Java :: resizing ImageIcon in JButton java 
Java :: get free player inventory slots spigot 
Java :: java show my form 
Java :: bulkwrite mongodb array of objects 
Java :: how to read a table from text file in java 
Java :: java foreach letter in word 
Java :: reverse arraylist java recursion 
Java :: Java Program to Change the Border of a JFrame: 
Java :: mettre caractère de string en majuscule java 
Java :: jsoup remove element 
Java :: Value Change Listener to JTextField 
Java :: if ternaire java 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =