Header Ad

Thursday, February 8, 2024

"org.springframework.http.converter.HttpMessageNotReadableException"

 Here is sample code


Controller class


@RequestMapping(method = RequestMethod.POST , consumes = "application/json")
    public ResponseEntity<String> welcome(
            @RequestBody DemoEntity demoEntity )
    {
        System.out.println(demoEntity.getName());
        String response ="success";
        return new ResponseEntity<>(response, HttpStatus.CREATED);
    }
}


Pojo Class

public class MyPojo implements Serializable {

    @JsonProperty("name")
    private String name;
    @JsonProperty("no")
    private int no;

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    MyPojo(String name)
    {
        this.name = name;
    }
}

Error


{
"timestamp": 1497594485418,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "JSON parse error: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value; nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value\n at [Source: java.io.PushbackInputStream@75be93a7; line: 1, column: 3]",
"path": "/welcome"
}




Solution

Incase if you are using Postman client to test your Rest API, there are chances wherein you must be adding body in under tab "form-data" rather "raw".







No comments:

Post a Comment