I’m currently trying to provide custom messages for exceptions, but ran into an issue with HttpMessageNotReadableException.
I have an ErrorDetails class:
public class ErrorDetails { private Date timestamp; private String message; private String details; public ErrorDetails(Date timestamp, String message, String details) { super(); this.timestamp = timestamp; this.message = message; this.details = details; } public Date getTimestamp() { return timestamp; } public void setTimestamp(Date timestamp) { this.timestamp = timestamp; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public String getDetails() { return details; } public void setDetails(String details) { this.details = details; }
I also have a custom exception handler:
@Order(Ordered.HIGHEST_PRECEDENCE) @ControllerAdvice @RestController public class CustomizedExceptionHandler extends ResponseEntityExceptionHandler { @ExceptionHandler(HttpMessageNotReadableException.class) @Override public final ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request){ ErrorDetails errorDetails = new ErrorDetails(new Date(), "hello",request.getDescription(true)); errorDetails.setMessage("Testing message"); return new ResponseEntity<>(errorDetails,HttpStatus.NOT_ACCEPTABLE); } }
But when i try to post a bad request, for example, with a field that should have a integer value I pass a string in the JSON it still returns the default error message of:
{ "timestamp": "2019-03-12T00:15:14.210+0000", "status": 400, "error": "Bad Request", "message": "JSON parse error: Cannot deserialize value of type `int` from String "lala": not a valid Integer value; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `int` from String "lala": not a valid Integer valuen at [Source: (PushbackInputStream); line: 5, column: 17] (through reference chain: com.tdl.model.ToDoNote["priority"])", "path": "/todos" }
The JSON request:
{ "name": "An workout", "dateToComplete": "Today", "description": "Sleep Day", "priority": "lala", "completed": false }
The desired effect would just be the test message appearing instead of the long description.
I also get this in my Eclipse console:
WARN 16508 — [nio-5000-exec-4] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type
int
from String “lala”: not a valid Integer value; nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of typeint
from String “lala”: not a valid Integer value at [Source: (PushbackInputStream); line: 5, column: 17] (through reference chain: com.tdl.model.ToDoNote[“priority”])]
I changed the status to NOT_ACCEPTABLE just to see more clearly if my custom error is returned.
Any help would be appreciated. Thank you.
EDIT
Added ExceptionHandler for InvalidFormatException, but nothing changed. I still get the default error(exception) message same as before.
@ExceptionHandler(InvalidFormatException.class) public final ResponseEntity<Object> handleInvalidFormat(InvalidFormatException ex, HttpHeaders headers, HttpStatus status, WebRequest request){ ErrorDetails errorDetails = new ErrorDetails(new Date(), "hello",request.getDescription(true)); errorDetails.setMessage("Testing message"); return new ResponseEntity<>(errorDetails,HttpStatus.NOT_ACCEPTABLE); }
Advertisement
Answer
The problem is solved. I had my custom exception classes in a badly named package. It was called just exception. While it should have been com.app.exception where the whole project is.