-
Notifications
You must be signed in to change notification settings - Fork 38.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AbstractMessageConverterMethodProcessor results in 406 even when ResponseEntity presets content type #23205
Comments
Fair point, in this case it's the server application that chose the content type of the response, and that choice could not be matched to a compatible converter. |
Thanks, @rstoyanchev ! Just want to ask one question: isn't In similar case several lines upper the if (body != null && producibleTypes.isEmpty()) {
throw new HttpMessageNotWritableException(
"No converter found for return value of type: " + valueType);
} This case is similar, except in this case no converters for return type found at all, while in new case no converters for [return type and required content-type] found. But, because the server is responsible for selecting this content-type, then I think Also, I think, it is worth to add P.S. Actually, while I writing this comment, I found another incorrect behavior. If you declare a controller like this: @GetMapping(path = "/xml", produces = "application/xml")
public Map<String, Object> producesContentTypeThatIsNotSupportedByAnyConverter() {
return ImmutableMap.of("foo", "bar");
} And make a request like this (httpie):
Then the result will be:
This is exactly the same case as with The |
As a follow-up to the recent commit #37f9ce, this change replaces the raised IllegalStateException with HttpMessageNotWritableException. See gh-23205
I've made a change to raise |
The fixes introduced in #20720 and #20798 added ability to return
ResponseEntity
with definedContentType
header. Like this:AbstractMessageConverterMethodProcessor
now takes into account providedContent-Type
header and uses it instead ofAccept
header from request. So it just ignoresAccept
header. It is ok and good.But in this case, when no
HttpMessageConverter
found that can write the response with selectedContent-Type
, thenAbstractMessageConverterMethodProcessor
throwsHttpMediaTypeNotAcceptableException
.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 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
selectedMediaType
is selected based onoutputMessage.getHeaders().getContentType()
(theAccept
header was not taken into account), of course. In other casesHttpMediaTypeNotAcceptableException
is still good.Affected version:
Spring 5.1.8.RELEASE
The text was updated successfully, but these errors were encountered: