Until Spring Framework v5.3.10, when Accept
header is not specified, all API used Content-Type: application/json
as default value. This behavior seems to have changed in v5.3.11 where Content-Type: application/xml
is being used.
Please refer attached demo.zip.
When following command is used to trigger HTTP request, different responses are returned by API with v5.3.10
and v5.3.11
.
curl --request GET --url http://localhost:8080/json
Spring Framework <= v5.3.10
{
"name": "Neo"
}
Spring Framework v5.3.11
<Json>
<name>Neo</name>
</Json>
Comment From: bclozel
I've tracked this change down to #27488.
When Spring MVC takes the producible media types and sorts them by specificity and quality, we're getting the following order in the sorted list:
// Spring Framework 5.3.10
0 = {org.springframework.http.MediaType@6104} "application/json"
1 = {org.springframework.http.MediaType@6104} "application/json"
2 = {org.springframework.http.MediaType@6107} "application/xml;charset=UTF-8"
3 = {org.springframework.http.MediaType@6110} "application/xml;charset=UTF-8"
4 = {org.springframework.http.MediaType@6105} "application/*+json"
5 = {org.springframework.http.MediaType@6106} "application/*+json"
6 = {org.springframework.http.MediaType@6108} "text/xml;charset=UTF-8"
7 = {org.springframework.http.MediaType@6109} "application/*+xml;charset=UTF-8"
8 = {org.springframework.http.MediaType@6111} "text/xml;charset=UTF-8"
9 = {org.springframework.http.MediaType@6112} "application/*+xml;charset=UTF-8"
// Spring Framework 5.3.11
0 = {org.springframework.http.MediaType@6101} "application/xml;charset=UTF-8"
1 = {org.springframework.http.MediaType@6102} "text/xml;charset=UTF-8"
2 = {org.springframework.http.MediaType@6104} "application/xml;charset=UTF-8"
3 = {org.springframework.http.MediaType@6105} "text/xml;charset=UTF-8"
4 = {org.springframework.http.MediaType@6098} "application/json"
5 = {org.springframework.http.MediaType@6098} "application/json"
6 = {org.springframework.http.MediaType@6103} "application/*+xml;charset=UTF-8"
7 = {org.springframework.http.MediaType@6106} "application/*+xml;charset=UTF-8"
8 = {org.springframework.http.MediaType@6099} "application/*+json"
9 = {org.springframework.http.MediaType@6100} "application/*+json"
This explains the behavior change you're seeing.
Arguably, not expressing any opinion in the request nor on the controller handler exposed you to an order that's outside of your control and both 5.3.10 or 5.3.11 behaviors are acceptable with the opinions expressed here.
The new ordering looks fine and predictable to me and I don't know how we can roll this back without breaking the fix made in #27488. I'll let @poutsma comment here in case I've missed something, but otherwise I think this issue should be closed.
Comment From: poutsma
@bclozel is correct in saying that the problem at hand is not expressing any media type preference, either through Accept header, suffix, or request parameter (see https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-config-content-negotiation). If you have any control over the client code, I would strongly suggest to improve this. Or, if you simply don't want to return XML, to disable XML support in your configuration.
That said, we do recognize that changing the default content-type in a minor release can be inconvenient, so we have reverted the changes that caused this issue.