Spring Boot return JSON and XML from the same endpoint is a common requirement when building REST APIs. In this tutorial, you’ll learn how to configure a single endpoint to return either format based on the client’s request using Content Negotiation — with real code examples and frequently asked interview questions.
Can a Spring Boot REST endpoint return both JSON and XML?
Yes. You don’t need two different methods or controllers. Spring Boot allows you to configure a single endpoint to return both JSON and XML responses using the produces attribute. The returned format depends on the client’s Accept header.
How does Spring Boot decide whether to return JSON or XML?
Spring uses Content Negotiation. It checks the value of the Accept header in the request. For example:
- Accept: application/json → returns JSON
- Accept: application/xml → returns XML
Example: Controller that returns both JSON and XML
@RestController
@RequestMapping("/accounts")
public class AccountController {
@GetMapping(produces = {
MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE
})
public List<Account> getAccounts() {
return List.of(
new Account(1, "Alice"),
new Account(2, "Bob")
);
}
}
📦 How should the model be annotated for XML support?
To enable XML output, annotate your model class using JAXB annotations like @XmlRootElement.
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Account {
private int id;
private String name;
// Required: No-arg constructor
public Account() {}
public Account(int id, String name) {
this.id = id;
this.name = name;
}
// Getters and setters
}
Without these annotations or the no-arg constructor, Spring Boot will fail to convert the object to XML.
⚙️ What dependencies are required for XML?
For JSON, no extra dependencies are needed — Spring Boot includes Jackson by default.
For XML, add the following to your pom.xml:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
🔍 What is the default response type?
If the client doesn’t send an Accept header, Spring Boot defaults to returning JSON.
🔄 If multiple values are specified, which one is used by default?
Spring Boot uses the first one listed in the produces array if no Accept header is sent. So, this matters:
@GetMapping(produces = {
MediaType.APPLICATION_JSON_VALUE, // This takes priority
MediaType.APPLICATION_XML_VALUE
})
To make XML the default, just switch the order.
🧼 How can I prettify the XML output?
Use a custom XmlMapper bean to enable pretty-printing:
@Bean
public XmlMapper xmlMapper() {
XmlMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
return mapper;
}
🧾 Can I use @RequestMapping instead of @GetMapping ?
Yes, both work:
@RequestMapping(
value = "/accounts",
produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }
)
But @GetMapping is more concise and preferred for GET requests.
💬 Can I return a ResponseEntity instead?
Absolutely! Using ResponseEntity gives you control over status codes and headers:
return ResponseEntity.ok().body(accounts);
✅ Summary: Key Steps to Support JSON and XML
To support both formats from one endpoint:
- ✅ Annotate your model with @XmlRootElement
- ✅ Add jackson-dataformat-xml to pom.xml
- ✅ Use produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE }
- ✅ Let Spring Boot handle content negotiation
Leave a Reply