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
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 noproducesspecified 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-Typeof the returned entity.Problem
The
HttpEntityMethodProcessordoes add the specifiedContent-Typeheader to theoutputHeaders(line184); however the call toAbstractMessageConverterMethodProcessor.writeWithMessageConvertersdoes not pay attention to this header's presence.getProducibleMediaTypesthen falls through to looking at theproducespart of the@RequestMappingannotation (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 fromgetProducibleMediaTypes).I'm not 100% certain this is a bug, but it certainly makes using the
ResponseEntity.BodyBuilder.contentTypemethod appear misleading in this context.Tested on both
4.3.7.RELEASEand4.3.12.RELEASE.Affects: 4.3.7, 4.3.12
Issue Links:
Referenced from: commits 9a894ab