-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Description
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:
- WebMVC RequestMapping Produces Problem [SPR-16448] #20993 WebMVC RequestMapping Produces Problem ("is duplicated by")
- Breaking change in MVC: Accept handling is now more strict [SPR-16251] #20798 Breaking change in MVC: Accept handling is now more strict
Referenced from: commits 9a894ab