Issue
I have a method on a rest service defined like this:
public ResponseEntity<?> methodName(@RequestParam("date") @DateTimeFormat(pattern = "yyyy-MM-dd") Date date,
)
When the date parameter is set to an invalid date such as 2020-3-999, the response from the service is:
{ "status": "error", "message": "Malformed request" }
I need a way to customize what appears in the "message" field of this return message..
Solution
You can create a Custom Error Handling class which should be annotated with @ControllerAdvice and extend ResponseEntityExceptionHandler.
Inside the class you can handle the exception that is occurred in your scenario and respond with a custom Error message.
Refer to https://www.baeldung.com/global-error-handler-in-a-spring-rest-api for implementation details.
Answered By - Ganesh chaitanya
Answer Checked By - Pedro (JavaFixing Volunteer)