Skip to content
Advertisement

Unable to use custom HttpMessageNotReadableException error message in Spring Boot

I’m currently trying to provide custom messages for exceptions, but ran into an issue with HttpMessageNotReadableException.

I have an ErrorDetails class:

JavaScript

I also have a custom exception handler:

JavaScript

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:

JavaScript

The JSON request:

JavaScript

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 type int 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.

JavaScript

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.

Advertisement