Content Negotiation in Spring MVC — JSON, XML, and Custom Media Types
Fast recall
Quick revision
Core answer
Whether the candidate understands how the Accept header drives response format selection through HttpMessageConverter.
Assuming @RestController always returns JSON — if an XML converter is on the classpath, clients requesting Accept: application/xml get XML.
Explain the three negotiation strategies (Accept header, URL extension, query parameter) and how produces attribute on @RequestMapping locks the format.
Interview-ready guide
Interview answer
Content negotiation is how Spring MVC decides the format of the response — JSON, XML, or something custom — based on what the client declares it can accept.
How negotiation works
- Client sends
Accept: application/jsonorAccept: application/xmlin the request header - Spring walks the registered
HttpMessageConverterlist and finds the first one that can produce the requested media type for the return type - If no converter matches, Spring returns
406 Not Acceptable
Default converters
- JSON:
MappingJackson2HttpMessageConverter(fromspring-boot-starter-web) - XML:
MappingJackson2XmlHttpMessageConverter(addjackson-dataformat-xml) orJaxb2RootElementHttpMessageConverter - Strings, byte arrays, multipart: built-in converters registered automatically
The `Accept: */*` trap
- A browser sends
Accept: */*— it will accept anything - If XML converter is registered and appears first, browsers may receive XML instead of JSON
- Fix: use
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE)to pin the response type, or register JSON converter first inconfigureMessageConverters()
For APIs that only ever produce JSON, pin produces = "application/json" at the controller class level — explicit is better than relying on converter ordering.
Content Negotiation in Spring MVC — JSON, XML, and Custom Media Types
Direct Answer
Content negotiation lets a single endpoint return different formats (JSON, XML, CSV) based on the client's Accept header. Spring MVC's HttpMessageConverter chain selects the converter matching the requested media type. Spring Boot auto-configures JSON support via Jackson; XML requires adding jackson-dataformat-xml to the classpath.
Common Mistakes & Pitfalls
Assuming @RestController always returns JSON — if an XML converter is on the classpath, clients requesting Accept: application/xml get XML.
Check your understanding
Answer these without looking back at the article.
- What are the trade-offs when applying Content Negotiation in Spring MVC — JSON, XML, and Custom Me in production?
- What are the trade-offs when applying Content Negotiation in Spring MVC — JSON, XML, and Custom Me in production?
- What are the trade-offs when applying Content Negotiation in Spring MVC — JSON, XML, and Custom Me in production?
Related interview prep categories
1Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.