'How to convert value of type String to Long using Spring Boot REST API

I made a Restful API with Spring Boot. One of my methods is a delete that takes the Response object and does some other queries eventually deleting the object from the DB depending on conditions. However, it is failing before the function body due to a type mismatch trying to convert String to Long.

I am using JSON in the request which does not use the Long type, so I use a string like this "id":"1" instead, which won't convert for some reason. Does anyone have a suggestion?

Response Object:

public class Response {

    private long id;
    private String key;
    private String value;
    private String group;
 
 
    public Response(String key, String value, String group) {
         this.key = key;
         this.value = value;
         this.group = group;
    }

I use a GeneratedValue(strategy = GenerationType.IDENTITY) to auto-create IDs.

It seems to fail when attempting to convert the JSON to a Response object:

public ResponseEntity<String> deleteResponse(@RequestBody Response response) {

}


Solution 1:[1]

You should not pass "id":"1" if you want auto-conversion. You should pass the JSON using "id": 1 (without quotes on the number).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 the Tin Man