Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Build Spring ResponseEntity object from JSON string

I have a valid JSON string of a ResponseEntity.class. Like this one:

{
    "headers": {
        "Location": ["/v1/books/12345"]
    },
    "body": {
        "id": 12345,
        "type": "BOOK",
        "address": {
            "address": "Some address",
            "city": "Some city"
        }
    },
    "statusCode": "CREATED",
    "statusCodeValue": 201
}

I would like to create a ResponseEntity.class object from that string. I am using a standard Jackson mapper for that:

objectMapper.readValue(jsonString, ResponseEntity.class)

And get back a following error:

Can not construct instance of org.springframework.http.ResponseEntity: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)

Indeed, when I checked the class, I did not see a default constructor there. How can I solve the issue and create an object?

like image 294
Dmitri Avatar asked Mar 17 '26 23:03

Dmitri


1 Answers

Jackson requires either a no arg constructor, or metadata to guide it. see http://www.cowtowncoder.com/blog/archives/2011/07/entry_457.html

To get this to work with ResponseEntity, you'd probably need to extend ResponseEntity, re-declare the constructors (delegating to super) and annotate them as Jackson expects.

like image 145
Taylor Avatar answered Mar 19 '26 13:03

Taylor