Skip to content

ResponseEntityResultHandler does not overwrite existing response headers [SPR-17082] #21619

@spring-projects-issues

Description

@spring-projects-issues

Brian Clozel opened SPR-17082 and commented

The current implementation of the ResponseEntityResultHandler only writes each ResponseEntity response header on the response only if there's none already for that header name.

This behavior is not consistent with what's being done in Spring MVC with the HttpEntityMethodProcessor - it also creates issues for some use cases (see reference URL for an example).

Given the following code snippet:

@GetMapping(path="/test", produces="text/plain")
@ResponseBody
public Mono<String> handler() {
   return Mono.error(new IllegalArgumentException("error")); 
}

@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<String> errorhandler(IllegalArgumentException ex) {
  return ResponseEntity.badRequest()
             .contentType(MediaType.APPLICATION_JSON)
             .body(jsonBody);
}

 In such a case, here's what happens:

  1. the handler is selected and invoked
  2. the text/plain content-type handler is written to the response
  3. it's getting an error signal instead of the response body
  4. the exception handler is invoked, all response entity headers are applied to the response, expect the ones that are already present in the response
  5. Content-Type is already there, so it's not written and the client will get an HTTP 406 instead

Affects: 5.0.7

Reference URL: spring-projects/spring-boot#13635

Issue Links:

Referenced from: commits 195f3f0, 407bd96

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions