Quick revision
The short answer
Once jackson-dataformat-xml is on the classpath, Spring registers an XML converter, so application/xml becomes producible for every endpoint. Clients whose Accept header ranks XML above everything else, such as browsers, or who send no clear preference while the XML converter sits first in the list, now get XML. We fix it by declaring produces = MediaType.APPLICATION_JSON_VALUE, setting JSON as the default content type, or removing the dependency if XML was never wanted.
What to remember
In this answer, we saw why a new jackson-dataformat-xml dependency changes responses: it makes application/xml producible, and clients with a wildcard Accept or a browser-style preference for XML now receive it, especially when the XML converter comes first in the list. We fixed it by declaring produces = MediaType.APPLICATION_JSON_VALUE, by setting JSON as the default content type, or by removing the dependency, and we added a test to catch it next time.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Once jackson-dataformat-xml is on the classpath, Spring registers an XML converter, so application/xml becomes producible for every endpoint. Clients whose Accept header ranks XML above everything else, such as browsers, or who send no clear preference while the XML converter sits first in the list, now get XML. We fix it by declaring produces = MediaType.APPLICATION_JSON_VALUE, setting JSON as the default content type, or removing the dependency if XML was never wanted.
Why this question is asked
This is a classic surprise: nobody touched the controllers, a library upgrade pulled in jackson-dataformat-xml as a transitive dependency, and a mobile app or a partner integration starts failing because it receives XML. Interviewers like it because the fix is easy once we understand content negotiation, and confusing if we do not. In this answer, we will reproduce the problem, see exactly why the format changes for some clients and not others, and walk through three fixes, from the most targeted to the simplest.
1A Quick Recap of How the Format Is Chosen
Spring MVC writes a return value through an HttpMessageConverter. Each converter knows which media types it can write, such as application/json for the Jackson converter. For every request, Spring compares the types listed in the Accept header with the types the converters can produce, and picks the best match by the client's q value (a weight between 0 and 1) and by specificity.
The key detail is what happens with a wildcard. If the client sends Accept: */* or no Accept header at all, every producible type is compatible, so the client has expressed no real preference. Spring then falls back on the order of the converters: the first one that can write the object decides the format.
2Reproducing the Problem
Before the change, only the Jackson JSON converter could write our Order. After a dependency brings in jackson-dataformat-xml, a second converter appears. We can see the effect with curl.
| Client sends | Before | After |
|---|---|---|
| Accept: application/json | JSON | JSON |
| Browser default (xml;q=0.9, */*;q=0.8) | JSON | XML |
| Accept: */* or no Accept, XML converter first | JSON | XML |
| Accept: */* or no Accept, JSON converter first | JSON | JSON |
Notice that clients that ask clearly for JSON are not affected. The ones that break are the clients that leave the choice to the server, or that prefer XML without meaning to. A browser is the typical example: its default Accept header gives application/xml a weight of 0.9 and */* only 0.8, so as soon as XML is producible, XML wins. Some HTTP client libraries also build their Accept header from all the converters they have, which can put application/xml first.
3Why the Converter Order Matters
In plain Spring MVC, the default converter list registers the Jackson XML converter before the Jackson JSON converter. So with a wildcard Accept, XML comes first and wins. Spring Boot builds its converter list a little differently and moves the XML converters towards the end, so in many Boot versions a plain */* still returns JSON (check your version). But a custom configureMessageConverters or extendMessageConverters in a WebMvcConfigurer can change that order again, and a browser-style Accept header gets XML regardless of order because it states a preference for it.
So the root cause is not a bug in Spring. The server's set of producible formats changed, and some clients had never said which format they actually wanted. The fix is to say it for them, on the server.
4Fix 1: Declare What the Endpoint Produces
The most precise fix is to state on the mapping that the endpoint only produces JSON.
Now application/json is the only producible type for these handlers. A client with a wildcard gets JSON, and a client that insists on XML only gets 406, which is honest: we do not offer XML. Putting produces at class level covers all methods of the controller at once.
5Fix 2: Set a Default Content Type
If there are many controllers, we can set the default format once. The default is used when the client expresses no preference, meaning no Accept header or only */*.
But there is a catch: the default only fills in for a missing preference. A browser that names application/xml with q=0.9 has a preference, so it still receives XML. That is why Fix 1 is stronger for public APIs, and why many teams combine both.
6Fix 3: Remove the Dependency
If nobody needs XML, the simplest fix is to keep jackson-dataformat-xml off the classpath. We find the library that pulled it in with mvn dependency:tree or gradle dependencies, and exclude it from that dependency. Then XML is no longer producible, and every client goes back to JSON. We should add a test that calls an endpoint with a browser-style Accept header and asserts Content-Type: application/json, so the next transitive surprise fails the build instead of a client.
7Follow-up questions
Why did clients that send Accept: application/json keep working?
Because they stated a clear preference. Spring found application/json among the producible types and used it, no matter how many other converters were registered. Only clients relying on wildcards or preferring XML were affected, which is why the problem looks random at first.
Can we keep XML for one partner and JSON for everyone else?
Yes. We keep the XML library, declare produces with both types on the endpoints the partner uses, and set JSON as the default content type. The partner sends Accept: application/xml and gets XML; everyone else, including wildcards, gets JSON. A test for each case keeps it that way.
How do we prove which converter wrote the response?
We can look at the Content-Type of the response, and for more detail turn on DEBUG logging for org.springframework.web.servlet.mvc.method.annotation, which logs the selected media type and the converter. In a test, MockMvc with an Accept header and an assertion on content().contentType(...) shows the result directly.
8Conclusion
In this answer, we saw why a new jackson-dataformat-xml dependency changes responses: it makes application/xml producible, and clients with a wildcard Accept or a browser-style preference for XML now receive it, especially when the XML converter comes first in the list. We fixed it by declaring produces = MediaType.APPLICATION_JSON_VALUE, by setting JSON as the default content type, or by removing the dependency, and we added a test to catch it next time.
Open with
When jackson-dataformat-xml lands on the classpath, Spring registers a Jackson XML converter, so every endpoint can suddenly produce application/xml as well as JSON. Spring picks the response format by matching the Accept header against the producible types.
Then, if they want more
Clients that ask for application/json explicitly are fine. The ones that change are clients without a clear preference. With Accept */* or no Accept header, every type matches, so the converter order decides, and in plain Spring MVC the XML converter is registered before JSON.
Spring Boot tends to move XML converters to the end, but custom converter configuration can undo that. And browsers send application/xml with q=0.9 and */* with 0.8, so they get XML whatever the order. To fix it, I would first find out how the dependency arrived with dependency:tree. If we do not need XML, I would exclude it.
If we do, I would declare produces = MediaType.APPLICATION_JSON_VALUE on the controllers that are JSON only, and set JSON as the default content type in configureContentNegotiation for clients without a preference. Then I would add a test with a browser-style Accept header that asserts JSON comes back.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "After adding a dependency that brings jackson-dataformat-xml, some clients start...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/4c562edd04/