Introduction
Here I am going to show you an example on how to return different data formats from REST API using Spring Boot framework. You may have a requirement that you need to return multiple formats, such as XML, JSON, etc., from the REST response, then this example may help you.
This example will return XML and JSON format depending on your HTTP settings. By default the REST will send you the JSON response. You need to set key Accept
‘s value in HTTP Headers to text/xml
or application/xml
to get the response in XML format.
Prerequisites
Java at least 8, Gradle 6.5.1 – 6.8.3, Maven 3.6.3, Spring Boot 2.4.0 – 2.4.5
Project Setup
You can create either gradle or maven based project in your favorite tool or IDE. The name of the project is spring-rest-return-different-data-format.
For gradle based project use the following build.gradle script:
buildscript {
ext {
springBootVersion = '2.4.0' to 2.4.5
}
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java-library'
id 'org.springframework.boot' version "${springBootVersion}"
}
sourceCompatibility = 12
targetCompatibility = 12
repositories {
mavenCentral()
jcenter()
}
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web:${springBootVersion}")
//required for JDK 9 or above for XML response
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.11.3'
implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
implementation 'org.glassfish.jaxb:jaxb-runtime:3.0.0-M5'
}
For maven based project use the following pom.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.roytuts</groupId>
<artifactId>spring-rest-return-different-data-format</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>12</maven.compiler.source>
<maven.compiler.target>12</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>
<!--required for JDK 9 or above for XML response -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Response Object
The DTO class that will be used as a response object in JSON or XML format for this example.
package com.roytuts.spring.rest.different.data.format.model;
public class Employee {
private String name;
private String designation;
public Employee() {
}
public Employee(String name, String designation) {
this.name = name;
this.designation = designation;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
@Override
public String toString() {
return "Employee [name=" + name + ", designation=" + designation + "]";
}
}
Rest Controller
The Spring REST controller class that defines the endpoints in various ways to return the response in XML and JSON formats using ResponseEntity
or without using ResponseEntity
class (using raw response class above).
package com.roytuts.spring.rest.different.data.format.rest.controller;
import java.util.Arrays;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.roytuts.spring.rest.different.data.format.model.Employee;
@RestController
public class SpringRestController {
@GetMapping(value = "/entityEmployees", produces = { MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE })
public ResponseEntity<List<Employee>> getEmployeesEntity() {
return new ResponseEntity<List<Employee>>(employees(), HttpStatus.OK);
}
@GetMapping(value = "/employees", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE,
MediaType.TEXT_XML_VALUE })
public List<Employee> getEmployees() {
return employees();
}
@GetMapping(value = "/entityEmployee", produces = { MediaType.APPLICATION_JSON_VALUE,
MediaType.APPLICATION_XML_VALUE, MediaType.TEXT_XML_VALUE })
public ResponseEntity<Employee> getEmployeeEntity() {
return new ResponseEntity<Employee>(new Employee("John", "Developer"), HttpStatus.OK);
}
@GetMapping(value = "/employee", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE,
MediaType.TEXT_XML_VALUE })
public Employee getEmployee() {
return new Employee("John", "Developer");
}
private List<Employee> employees() {
List<Employee> employees = Arrays.asList(new Employee("John", "Developer"),
new Employee("Michel", "Sr Developer"), new Employee("Harris", "Developer"),
new Employee("Kamla", "Sr Developer"), new Employee("Jerome", "Manager"));
return employees;
}
}
Main Class
A class having main method with @SpringBootApplication
will deploy the application into embedded Tomcat server.
package com.roytuts.spring.rest.different.data.format;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringRestDataFormatsApp {
public static void main(String[] args) {
SpringApplication.run(SpringRestDataFormatsApp.class, args);
}
}
Testing the Application
I am using Postman tool to test the REST API. You can use any REST client to consume the REST services.
XML Response
URL: http://localhost:8080/employees
HTTP Method: GET
HTTP Headers: Key -> Accept, Value -> application/xml or text/xml
<List>
<item>
<name>John</name>
<designation>Developer</designation>
</item>
<item>
<name>Michel</name>
<designation>Sr Developer</designation>
</item>
<item>
<name>Harris</name>
<designation>Developer</designation>
</item>
<item>
<name>Kamla</name>
<designation>Sr Developer</designation>
</item>
<item>
<name>Jerome</name>
<designation>Manager</designation>
</item>
</List>
Here is the image that shows what you need to configure to get the response in XML format.

JSON Response
URL: http://localhost:8080/employees
HTTP Method: GET
[
{
"name": "John",
"designation": "Developer"
},
{
"name": "Michel",
"designation": "Sr Developer"
},
{
"name": "Harris",
"designation": "Developer"
},
{
"name": "Kamla",
"designation": "Sr Developer"
},
{
"name": "Jerome",
"designation": "Manager"
}
]