The fixes introduced in #20720 and #20798 added ability to return ResponseEntity with defined ContentType header. Like this:
@GetMapping("/test")
public ResponseEntity<MyModel> test() {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_XML).body(new MyModel());
}
AbstractMessageConverterMethodProcessor now takes into account provided Content-Type header and uses it instead of Accept header from request. So it just ignores Accept header. It is ok and good.
But in this case, when no HttpMessageConverter found that can write the response with selected Content-Type, then AbstractMessageConverterMethodProcessor throws HttpMediaTypeNotAcceptableException.
if (body != null) {
throw new HttpMediaTypeNotAcceptableException(this.allSupportedMediaTypes);
}
I think this is not right, because it leads to 406 response, like if no acceptable representation was found, while the real reason of this error is that no converter found for selected representation (and Accept header was ignored altogether). So, this clearly indicates a server misconfiguration (server tries to return a response using provided content type without registering proper HttpMessageConverter, that supports this content type with this response body type). So it should lead to error 500 and should throw HttpMessageNotWritableException.
It should do this only when selectedMediaType is selected based on outputMessage.getHeaders().getContentType() (the Accept header was not taken into account), of course. In other cases HttpMediaTypeNotAcceptableException is still good.
Affected version:
Spring 5.1.8.RELEASE
The fixes introduced in #20720 and #20798 added ability to return
ResponseEntitywith definedContentTypeheader. Like this:AbstractMessageConverterMethodProcessornow takes into account providedContent-Typeheader and uses it instead ofAcceptheader from request. So it just ignoresAcceptheader. It is ok and good.But in this case, when no
HttpMessageConverterfound that can write the response with selectedContent-Type, thenAbstractMessageConverterMethodProcessorthrowsHttpMediaTypeNotAcceptableException.I think this is not right, because it leads to 406 response, like if no acceptable representation was found, while the real reason of this error is that no converter found for selected representation (and
Acceptheader was ignored altogether). So, this clearly indicates a server misconfiguration (server tries to return a response using provided content type without registering properHttpMessageConverter, that supports this content type with this response body type). So it should lead to error 500 and should throwHttpMessageNotWritableException.It should do this only when
selectedMediaTypeis selected based onoutputMessage.getHeaders().getContentType()(theAcceptheader was not taken into account), of course. In other casesHttpMediaTypeNotAcceptableExceptionis still good.Affected version:
Spring 5.1.8.RELEASE