Home โบ Java exceptions โบ HttpMessageNotReadableException
HttpMessageNotReadableException: causes and fixes
Spring MVC could not read the request body: invalid JSON, missing body or a type mismatch.
What it looks like
org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.lang.Integer` from String "abc"Extends HttpMessageConversionException in org.springframework.http.converter.
What it means
Spring MVC converts the request body to your @RequestBody parameter with an HttpMessageConverter, usually Jackson. If the body cannot be read, the converter fails and Spring raises this exception, which the default handler turns into a 400 Bad Request. The message includes Jackson's explanation (and the line and column in the body), or says "Required request body is missing" when no body was sent at all.
Common causes
- Malformed JSON: a trailing comma, single quotes, an unescaped character.
- A value of the wrong type for a field, such as text for a number or a bad date format.
- An empty body for an endpoint with a required
@RequestBody, or a wrong Content-Type header. - Unknown properties when
FAIL_ON_UNKNOWN_PROPERTIESis on, or a missing constructor or creator for a record or immutable class.
How to fix it
- Reproduce with curl and check the JSON with a validator; read the line and column in the message.
- Send
Content-Type: application/jsonand the right date format (ISO 8601 by default). - Handle the exception in a
@ControllerAdviceand return a readable error body. - Make the request class deserialisable: default constructor,
@JsonCreator, or a record.
Broken and fixed
curl -X POST /orders -d "{name: bob,}" # invalid JSONcurl -X POST /orders -H "Content-Type: application/json" -d '{"name":"bob"}'๐ Unravel the failing code ๐ Unravel the fix
In Spring and enterprise code
Spring Boot sets FAIL_ON_UNKNOWN_PROPERTIES to false by default, so extra fields are ignored unless you changed it.
Debug it faster
Paste the whole stack trace into the stack trace beautifier to hide framework frames, jump to the root cause and find the first line of your own code. It runs in your browser, so the trace never leaves your machine.
Frequently asked questions
Is HttpMessageNotReadableException a checked or unchecked exception?
HttpMessageNotReadableException is an unchecked exception (it extends HttpMessageConversionException, which is a RuntimeException), so you are not forced to catch it. Prevent it with validation, or handle it where the input enters the system.
Which package is HttpMessageNotReadableException in?
It lives in org.springframework.http.converter, so the import is import org.springframework.http.converter.HttpMessageNotReadableException;.
What is the first thing to check when I see HttpMessageNotReadableException?
Read the message and the first stack trace line in your own code. The usual causes are: Malformed JSON: a trailing comma, single quotes, an unescaped character; and A value of the wrong type for a field, such as text for a number or a bad date format.