3

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?

2
  • 2
    why do you need to convert it to ResponseEntity object? Commented Jan 25, 2018 at 19:42
  • 1
    @gohil90 String represents a cached response. If certain conditions are met, then I would like to return it directly from controller without doing any calculations. Commented Jan 25, 2018 at 19:44

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Tried that out, but did not succeeded. If you could provide an example, that would be perfect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.