Spring Boot REST API - Returning Response in XML Format
Introduction
In this guide, we will learn how to return a response in XML format from a Spring Boot REST API. By default, Spring Boot returns responses in JSON, but we can configure it to return XML by following a few simple steps. This is particularly useful when integrating with systems that require XML responses, such as legacy applications or third-party services.
Why Use XML in REST APIs?
- Interoperability: Many older systems and enterprise applications still rely on XML for data exchange.
- Structured Data: XML provides a well-structured format that is widely used in web services (SOAP-based services).
- Customization: XML allows for defining custom schemas and DTDs, making data validation more flexible.
Steps to Implement XML Response in Spring Boot
1️⃣ Add Required Dependency
Spring Boot uses Jackson for JSON serialization and deserialization. To enable XML support, we need to include jackson-dataformat-xml
dependency in pom.xml
:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
This dependency enables Jackson to convert Java objects into XML format automatically.
2️⃣ Create a Model Class (DTO) for XML Response
We need a model class (DTO) that represents the response data. Annotate it with @XmlRootElement
and @XmlAccessorType
to enable XML conversion.
package com.example.demo.dto;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlAccessorType;
import jakarta.xml.bind.annotation.XmlAccessType;
@XmlRootElement(name = "User")
@XmlAccessorType(XmlAccessType.FIELD)
public class UserResponse {
@XmlElement
private String name;
@XmlElement
private int age;
@XmlElement
private String email;
// Default Constructor
public UserResponse() {}
// Parameterized Constructor
public UserResponse(String name, int age, String email) {
this.name = name;
this.age = age;
this.email = email;
}
// Getters and Setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
}
3️⃣ Create a REST Controller
Next, we create a Spring Boot REST Controller that returns the response in XML format. We use produces = MediaType.APPLICATION_XML_VALUE
to specify XML response type.
package com.example.demo.controller;
import com.example.demo.dto.UserResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping(value = "/user", produces = MediaType.APPLICATION_XML_VALUE)
public UserResponse getUser() {
return new UserResponse("Ayush", 25, "ayush@example.com");
}
}
4️⃣ Create Main Spring Boot Application Class
The main class is needed to start the Spring Boot application.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
5️⃣ Run the Application and Test the API
Start the Spring Boot application, then send a GET request using Postman, Browser, or Curl:
GET http://localhost:8080/api/user
6️⃣ Expected XML Response Output
When the API is called, it returns the response in XML format as shown below:
<User>
<name>Ayush</name>
<age>25</age>
<email>ayush@example.com</email>
</User>
Troubleshooting Common Issues
✅ Error: XML Response Not Showing?
- Ensure that the
jackson-dataformat-xml
dependency is added. - Check if
produces = MediaType.APPLICATION_XML_VALUE
is correctly set in the controller. - Use Postman and check the
Accept
header; set it toapplication/xml
.
✅ XML Response Showing as JSON?
- Spring Boot defaults to JSON; explicitly request XML by setting the
Accept
header toapplication/xml
. - Make sure the model class is correctly annotated with
@XmlRootElement
and@XmlAccessorType
.
Summary
✅ Added Jackson XML dependency to support XML serialization.
✅ Created a DTO class with @XmlRootElement
to define the XML structure.
✅ Developed a REST API that returns XML response using produces = MediaType.APPLICATION_XML_VALUE
.
✅ Ran and tested the API to verify XML response output.
Conclusion
Using XML in Spring Boot REST APIs is straightforward and useful when working with applications that require XML data exchange. With proper configuration, we can seamlessly switch between JSON and XML responses depending on the client’s request.
Top comments (0)