Java Spring - How to fix HttpMessageNotWritableException Infinite Recursion JSON response?

4.4k views Asked by At

So I have a simple method in java spring that returns a class as a ResponseBody:

@RequestMapping(value = "update", method = RequestMethod.PUT)
public @ResponseBody JKResponse update() {
    return new JKResponse();
}

And the JKResponse class is as follows:

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY, 
                isGetterVisibility = JsonAutoDetect.Visibility.ANY,
                getterVisibility = JsonAutoDetect.Visibility.ANY)
public class JKResponse implements Serializable {
    static final long serialVersionUID = 1L;

    @JsonProperty
    private List<String> stuff;

    public JKResponse() {
        this.stuff = new ArrayList<String>();
    }

    public void setStuff(final List<String> stuff) {
        this.stuff = stuff;
    }

    public List<String> getStuff() {
        return this.stuff;
    }

    /*
       THIS METHOD IS CAUSING THE INFINITE RECURSION, BUT WHY?
    */
    public ResponseEntity<JResponse> getResponseEntity() {
        return new ResponseEntity<JResponse>(this, HttpStatus.OK);
    }
}

But when I use postman to called the "update" API route, I get the following error:

Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: Infinite recursion (StackOverflowError) (through reference chain: org.springframework.http.ResponseEntity["body"]->com.proj.services.JKService["responseEntity"]->org.springframework.http.ResponseEntity["body"] and so on...

And the JSON displayed in postman is:

{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body":{"responseEntity":{"headers":{},"body": and so on...

And I can't figure out what is causing this, and why this is happening. Any advice?

1

There are 1 answers

0
name_no On

You don't usually need to send ResponseEntity to the user, ResponseEntity is the data object you use to tell Spring what it should return to the user. It is supposed to be used like this:

@RequestMapping(value = "update", method = RequestMethod.PUT)
public ResponseEntity<JKResponse> update () {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.set("Boiling", "True");
    return new ResponseEntity<JKResponse>(
            new JKResponse(), responseHeaders, HttpStatus.I_AM_A_TEAPOT);
}

And the user will get this:

$ curl -X PUT -i 127.0.0.1:8080/update
HTTP/1.1 418 
Server: Apache-Coyote/1.1
Boiling: True
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Tue, 29 Dec 2015 20:03:51 GMT

{"stuff":[]}