close

[Solved] How to handle “org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error” in Spring?

Hello Guys, How are you all? Hope You all Are Fine. Today I am facing the following error How to handle “org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error” in Spring? in Java. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How to handle “org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error” in Spring? Error Occurs?

Today I am facing the following error How to handle “org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error” in Spring? in Java.

How To Solve handle “org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error” in Spring? Error ?

  1. How To Solve How to handle “org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error” in Spring? Error ?

    To Solve How to handle “org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error” in Spring? Error This should work but Spring is not meant to handle error before the HTTP request can be deserialized correctly

  2. How to handle “org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error” in Spring?

    To Solve How to handle “org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error” in Spring? Error This should work but Spring is not meant to handle error before the HTTP request can be deserialized correctly

Solution 1

This should work but Spring is not meant to handle error before the HTTP request can be deserialized correctly. You can write rules on request data after deserialization using annotation on your DTO class’s fields like @NotNull, @NotEmpty, @Positive, …

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class ErrorController {
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<String> handleException(HttpMessageNotReadableException exception, HttpServletRequest request) {
        return new ResponseEntity("You gave an incorrect value for ....", HttpStatus.BAD_REQUEST);
    }
}

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read