Quick revision
The short answer
Spring reads the Accept header of the request, compares the media types the client will accept with the ones the handler can produce (its produces condition and the message converters that can write the return type), and picks the best match. JSON is always possible when Jackson is present, and XML becomes possible as soon as jackson-dataformat-xml is on the classpath. If nothing the client accepts can be produced, Spring answers 406 Not Acceptable.
What to remember
In this answer, we saw that Spring MVC negotiates the response format by comparing the media types in the Accept header with the types the converters can write, narrowed by produces. Adding jackson-dataformat-xml makes XML producible, a request for a type nobody can write ends in 406 Not Acceptable, path extensions like .json are no longer used by default, and a format query parameter can be enabled with spring.mvc.contentnegotiation.favor-parameter.
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.
Spring reads the Accept header of the request, compares the media types the client will accept with the ones the handler can produce (its produces condition and the message converters that can write the return type), and picks the best match. JSON is always possible when Jackson is present, and XML becomes possible as soon as jackson-dataformat-xml is on the classpath. If nothing the client accepts can be produced, Spring answers 406 Not Acceptable.
Why this question is asked
Every REST controller we write returns Java objects, yet clients receive bytes in some format. Deciding that format is called content negotiation, and most of the time it works so quietly that we forget it exists. It stops being quiet when a client suddenly gets XML, when a partner asks for a format we never planned, or when a 406 shows up in the logs. Interviewers ask this question to see whether we understand the path from return value to response body. In this answer, we will recap what message converters are, follow the negotiation step by step for GET /orders/1, see what happens when no format matches, and look at the two older ways of choosing a format: path extensions and a format query parameter.
1A Quick Recap of Message Converters
When a @RestController method returns an Order object, Spring MVC does not turn it into JSON itself. It hands the object to an HttpMessageConverter, a small strategy class that knows how to write one kind of Java object as one or more media types. A media type, also called a MIME type, is the label of a format, such as application/json or application/xml.
Spring Boot registers a list of converters at startup. There is one for String and byte arrays, one for resources, and the Jackson converter that writes any object as application/json. If the jackson-dataformat-xml library is on the classpath, Spring also registers a Jackson XML converter that writes the same objects as application/xml. So which formats our endpoint can produce depends partly on which libraries happen to be on the classpath.
The client states what it wants in the Accept request header, for example Accept: application/json, or a list with weights such as Accept: application/xml;q=0.9, */*;q=0.8. The q value, between 0 and 1, says how much the client prefers each type, and */* means any type at all.
2How Spring Picks the Format for GET /orders/1
Let us follow one request. The controller below declares no produces attribute, so it is willing to return whatever the converters can write.
- Read requested typesAccept header, for example application/xml
- Collect producible typesconverters that can write Order, narrowed by produces
- Any compatible pair?YesSort by q value and specificityNo406 Not Acceptable
- Pick the first concrete typeapplication/xml
- Converter writes the bodyJackson XML converter, Content-Type set
5 steps in order. Play them, or step through them one at a time.
First, the ContentNegotiationManager works out the requested media types. By default it uses only the Accept header. Next, Spring asks every converter which media types it can write for the Order class; these are the producible types. If the mapping declares produces, that list narrows the producible types. Then Spring pairs each requested type with each producible type, keeps the compatible pairs, and sorts them by the client's q value and by how specific the type is. The first concrete media type wins, and the converter that supports it writes the body.
Notice that a wildcard such as */* is compatible with everything, so the order of the converters decides the result in that case. That is why the same endpoint can answer JSON on one machine and XML on another after a dependency change.
3What Happens When No Format Matches
Suppose the client sends Accept: application/pdf, or Accept: application/xml while the XML library is not on the classpath. Spring finds requested types and producible types, but no pair is compatible. It throws HttpMediaTypeNotAcceptableException, and the default exception handling turns that into 406 Not Acceptable.
There is a small difference between the two cases. When the mapping declares produces, Spring rejects the request while it is still choosing a handler, so our method never runs. Without produces, the method runs, returns its Order, and only then does Spring discover that no converter can write the requested type. The result is 406 either way, but in the second case we have already done the work of loading the order. So declaring produces on endpoints with a fixed format is cheaper and also documents the contract.
4Path Extensions Are Gone, a Format Parameter Is Optional
Older Spring versions also looked at the end of the path: GET /orders/1.json asked for JSON and /orders/1.xml asked for XML. This suffix matching caused confusing mappings and even security issues, so it was deprecated in Spring Framework 5.3 and is off by default in Spring Boot and in Spring Framework 6 and later. Today /orders/1.json is simply a different path, which usually does not match at all.
If we want clients without control over headers, such as a link in a browser, to pick the format, we can turn on a query parameter instead.
With the parameter turned on, Spring checks it before the Accept header. The values json and xml map to their media types by default when the matching libraries are present, and we can register more with spring.mvc.contentnegotiation.media-types.yaml=application/yaml. For machine clients, the Accept header remains the standard way, and the parameter is a convenience on top.
5Follow-up questions
Why does Spring not simply look at the return type and always send JSON?
Because the return type is a Java class, not a format. The same Order can be written by several converters, and the HTTP protocol gives the client the right to ask for a representation. Spring respects that by negotiating, and we can still force JSON by declaring produces on the mapping or by not putting an XML library on the classpath.
Does the Content-Type request header affect which format is returned?
No. Content-Type describes the body the client sends, and Spring uses it to choose the converter that reads @RequestBody. If no converter can read it, the result is 415 Unsupported Media Type. The format of the response is driven by Accept, so a client can send JSON and ask for XML back.
What does a client get when it sends no Accept header at all?
Spring treats a missing Accept header as */*, so any producible type is compatible and the first suitable converter in the list wins. With only Jackson present that is JSON. We can make the choice explicit with a default content type in configureContentNegotiation, which applies whenever the client expresses no preference.
6Conclusion
In this answer, we saw that Spring MVC negotiates the response format by comparing the media types in the Accept header with the types the converters can write, narrowed by produces. Adding jackson-dataformat-xml makes XML producible, a request for a type nobody can write ends in 406 Not Acceptable, path extensions like .json are no longer used by default, and a format query parameter can be enabled with spring.mvc.contentnegotiation.favor-parameter.
Open with
Spring decides the format through content negotiation. The ContentNegotiationManager reads the requested media types, by default only from the Accept header. Then Spring asks each HttpMessageConverter which types it can write for the return type, here Order, and narrows that list with the produces attribute if the mapping has one.
Then, if they want more
It pairs requested and producible types, sorts the compatible ones by the client's q value and specificity, and uses the first concrete type. The Jackson converter gives us JSON, and if jackson-dataformat-xml is on the classpath, a Jackson XML converter is registered too, so XML becomes possible.
If the client asks for something nobody can produce, say application/pdf, Spring throws HttpMediaTypeNotAcceptableException and answers 406 Not Acceptable. With produces declared, that happens before the handler runs, which saves work. Path extensions such as /orders/1.json used to select the format, but suffix matching was deprecated in 5.3 and is off by default now.
If we need a format in the URL, we can enable a query parameter with spring.mvc.contentnegotiation.favor-parameter, and then ?format=xml chooses XML.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Framework 5, Spring Framework 6
- 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 "How does Spring decide whether to return JSON or XML for GET /orders/1, and what happens...": https://scrivio-interview-library.netlify.app/spring-boot/rest-controllers/2f06d84e03/