Skip to content

AbstractMessageConverterMethodProcessor ignores HttpEntityMethodProcessor's Content-Type header [SPR-16172] #20720

@spring-projects-issues

Description

@spring-projects-issues

Liam Bryan opened SPR-16172 and commented

Context:
We have a controller method which returns ResponseEntity<?> (because several conversions are supported based on the client's request). This means that there is no produces specified in the @RequestMapping (or more directly, @GetMapping) annotation.

Using:
return ResponseEntity.ok().contentType(...).body(...);

I believe it should be possible to directly specify the Content-Type of the returned entity.

Problem
The HttpEntityMethodProcessor does add the specified Content-Type header to the outputHeaders (line 184); however the call to AbstractMessageConverterMethodProcessor.writeWithMessageConverters does not pay attention to this header's presence. getProducibleMediaTypes then falls through to looking at the produces part of the @RequestMapping annotation (which is empty) and so declares support for everything. In our case this means that it ends up being handled by the wrong (or at least, from our perspective, not the intended) HttpMessageConverter.

Current workaround
We are getting around this at present by adding a call to HttpServletRequest.setAttribute(HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE, Collections.singleton(...)); (which then directly overrides the return value from getProducibleMediaTypes).

I'm not 100% certain this is a bug, but it certainly makes using the ResponseEntity.BodyBuilder.contentType method appear misleading in this context.

Tested on both 4.3.7.RELEASE and 4.3.12.RELEASE.


Affects: 4.3.7, 4.3.12

Issue Links:

Referenced from: commits 9a894ab

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