Skip to content

When a @RequestMapping Returns a Sublclass of ResponseEntity It Is Returning Header and StatusCode Along with the Body [SPR-10046] #14680

@spring-projects-issues

Description

@spring-projects-issues

Julian Builes opened SPR-10046 and commented

When subclassing ResponseEntity that returns a JSON response, the body in the resulting response is also including 'headers' and 'statusCode fields.

        class MyClass {      
            public String field;            
            public MyClass(String field1) {
                this.field = field1;
            }
        }
        
        class MyClassResponse extends ResponseEntity<MyClass> {
            public MyClassResponse(MyClass body, HttpHeaders headers, HttpStatus statusCode) {
                super(body, headers, statusCode);
            }
        }
        
        @RequestMapping(value = "aaa", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
        @ResponseBody
        public ResponseEntity<MyClass> dummy() {

            HttpHeaders headers = new HttpHeaders();
            headers.add("some-header", "some header value");
            headers.add("another-header", "another header value");

            return new MyClassResponse(new MyClass("this is the response body"), headers, HttpStatus.CREATED);
        }

Is returning:

{
"headers": {
"some-header": [(1)
"some header value"
],-
"another-header": [(1)
"another header value"
]-
},-
"body": {
"field": "this is the response body"
},-
"statusCode": "CREATED"
}

Considerations:

  • This appears to be a regression introduced between 3.1.2 and 3.1.3.
  • We are using Jackson to convert the JSON

Affects: 3.1.3

1 votes, 4 watchers

Metadata

Metadata

Assignees

Labels

status: invalidAn issue that we don't feel is valid

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions