Quick revision
The short answer
When jackson-dataformat-xml appears on the classpath, Spring Boot registers an XML message converter next to the JSON one. Spring MVC then chooses the response format from the Accept header, and browsers send an Accept header that lists application/xml with a higher weight than */*, so they get XML; clients sending only */* may get it too, depending on the order of the converters. We fix it by declaring produces = application/json on the endpoints, excluding the dependency if it came in transitively, or configuring content negotiation to prefer JSON, and we add a test with a browser-like Accept header.
What to remember
In this answer, we saw that adding jackson-dataformat-xml makes Spring Boot register an XML converter, that content negotiation follows the Accept header, and that browsers rank application/xml above */* while generic clients fall back to converter order, so both got XML. We fixed it by finding and excluding an unneeded transitive dependency, declaring produces = application/json, or configuring negotiation to prefer JSON, and we added a test with a browser-like Accept header.
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.
When jackson-dataformat-xml appears on the classpath, Spring Boot registers an XML message converter next to the JSON one. Spring MVC then chooses the response format from the Accept header, and browsers send an Accept header that lists application/xml with a higher weight than */*, so they get XML; clients sending only */* may get it too, depending on the order of the converters. We fix it by declaring produces = application/json on the endpoints, excluding the dependency if it came in transitively, or configuring content negotiation to prefer JSON, and we add a test with a browser-like Accept header.
Why this question is asked
This bug is confusing because no code changed. A dependency upgrade pulled in a new library, and suddenly some clients, often browsers and tools that send generic Accept headers, receive XML from an API that has only ever spoken JSON. Interviewers use it to see whether we understand Spring's message converters and content negotiation rather than treating them as magic. In this answer, we will recap how Spring MVC chooses a response format, reproduce the problem, explain why browsers get XML, and then go through the fixes and a test that keeps it from coming back.
1A Quick Recap of Message Converters and Content Negotiation
When a @RestController method returns an object, Spring MVC does not write JSON directly. It asks a list of HttpMessageConverters, objects that know how to write a Java object in one media type, which ones can write this object. The Jackson JSON converter writes application/json; the Jackson XML converter writes application/xml.
Content negotiation is how Spring picks among them. It reads the request's Accept header, a list of media types the client is willing to receive, each with an optional quality value q between 0 and 1. It intersects that list with the types the converters can produce, sorts by the client's preference, and uses the first converter that can write the chosen type. Spring Boot's auto-configuration adds the XML converter automatically as soon as jackson-dataformat-xml is on the classpath. In Spring MVC's own default list the XML converter is registered before the JSON one; Spring Boot moves XML converters towards the end of its list, so the order, and with it the answer for a */* request, can differ by setup. Check your version.
2Reproducing the Problem
Take a plain controller with no produces attribute and look at what three different clients receive after the dependency appeared.
Clients that ask explicitly for JSON still get JSON, which is why the problem often goes unnoticed in automated tests. Browsers, simple HTTP tools and some generated clients get XML.
3Why Browsers Get XML
A browser's Accept header is built for web pages. A typical one says text/html first, then application/xhtml+xml, then application/xml with q=0.9, and finally */* with q=0.8. Our controller cannot produce HTML, so Spring looks at the remaining entries. application/xml at 0.9 is preferred over */* at 0.8, and now there is a converter that can produce it, so Spring chooses XML. Before the upgrade, no XML converter existed, the only match was */* and Spring used the JSON converter.
For a client that sends */* or nothing at all, every producible type is equally acceptable. Spring then follows the order of the converters, so whichever converter comes first wins; in a plain Spring MVC setup that is the XML converter. So the root cause is the presence of the converter, and the visible symptom depends on each client's Accept header.
- Read the Accept headertext/html, application/xml;q=0.9, */*;q=0.8
- Find types the converters can produceapplication/json, application/xml
- Match and sort by client preferenceapplication/xml (0.9) beats */* (0.8)
- Is produces set on the endpoint?YesOnly application/json is producibleNoXML converter is chosen
- Write the body with the chosen converter
5 steps in order. Play them, or step through them one at a time.
Adding a converter changes the set of producible types, and with it the outcome for some clients.
4Fix 1: Find Out Why the Dependency Is There
The first question is whether we want XML at all. Running mvn dependency:tree -Dincludes=com.fasterxml.jackson.dataformat:jackson-dataformat-xml, or gradle dependencyInsight, shows which library brought the module in. Often it is a transitive dependency of a client SDK or a reporting library. If our application does not need XML, excluding it removes the converter and restores the old behaviour everywhere at once.
But there is a catch: the library that brought it may need it at runtime. If it does, excluding it breaks that library, and we need one of the next fixes instead.
5Fix 2: Declare What Each Endpoint Produces
The most explicit fix is to say what the API produces. With produces = MediaType.APPLICATION_JSON_VALUE, the only producible type for the endpoint is JSON, so content negotiation can only choose it. A browser's */* still matches, and the response is JSON. A client that asks only for application/xml gets 406 Not Acceptable, which is the honest answer. Putting the attribute on a class-level @RequestMapping covers all methods of the controller.
6Fix 3: Configure Content Negotiation and Test It
If we want XML available on request but JSON as the default, we can configure negotiation globally. A WebMvcConfigurer can set the default content type to JSON for requests without a usable Accept header, and can reorder the converters so the JSON converter comes before the XML converter, which makes JSON win for */*. Browsers that explicitly rank application/xml higher will still get XML in that setup, so for a JSON API the produces attribute or removing the converter remains the clearer answer. Ignoring the Accept header entirely is possible too, but then clients can no longer ask for XML at all.
The test is the part that prevents a repeat. Most API tests send Accept: application/json, which hides this class of bug completely. One test with a browser-like header and one with */* would have failed in the pull request that upgraded the dependency.
7Follow-up questions
Why did automated tests not catch this?
Because tests and generated clients usually send Accept: application/json, and for those requests nothing changed. Only clients with generic or browser Accept headers saw XML. That is why I add a test with a browser-like header and one with */* to API test suites.
Is returning XML to a browser actually wrong?
By HTTP rules, Spring behaved correctly: the browser said it prefers XML to anything else. It is wrong for our API contract, which promises JSON. The fix is to make the contract explicit with produces, so the negotiation result matches what we document.
What changes with Spring Boot 4 and Jackson 3?
The converters are the Jackson 3 versions with new class names, but the mechanism is the same: an XML converter is registered when the XML module is present, and negotiation follows the Accept header. Check your version for the exact class names when customizing the converter list.
8Conclusion
In this answer, we saw that adding jackson-dataformat-xml makes Spring Boot register an XML converter, that content negotiation follows the Accept header, and that browsers rank application/xml above */* while generic clients fall back to converter order, so both got XML. We fixed it by finding and excluding an unneeded transitive dependency, declaring produces = application/json, or configuring negotiation to prefer JSON, and we added a test with a browser-like Accept header.
Open with
Nothing in our code changed; the classpath did. When jackson-dataformat-xml is present, Spring Boot registers a Jackson XML message converter alongside the JSON one. Spring MVC picks the response format by content negotiation on the Accept header.
Then, if they want more
A browser sends something like text/html, application/xhtml+xml, application/xml with q 0.9 and star slash star with q 0.8. We cannot produce HTML, and now we can produce XML, which the browser prefers to anything else, so it gets XML.
Clients sending star slash star can get XML too, because then the first converter in the list that can write the object wins, and that depends on converter order. Clients asking for application/json are unaffected, which is why tests missed it.
To fix it, I would first run dependency tree to see who brought the XML module; if we do not need XML, I exclude it. Otherwise I declare produces application/json on the controllers, ideally at class level, so JSON is the only option and an XML-only request gets 406.
If we want XML on request but JSON by default, I configure content negotiation and converter order. And I add MockMvc tests with a browser-like Accept header and with star slash star, asserting the content type is JSON.
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 a dependency upgrade added jackson-dataformat-xml to the classpath, a Spring MVC...": https://scrivio-interview-library.netlify.app/spring-boot/json-serialization/f698b3d1ec/