In this article, we will learn how to produce both JSON and XML responses using Spring REST API.
1. Development Environment
- Eclipse
- JDK 1.8
- Tomcat 8.5
- Maven
2. Create a Maven Web Project
Below is my dynamic web project spring-mvc-rest-json-xml structure in eclipse. In this example, we are using spring 4.1.4.RELEASE version.
This project has a single endpoint and which will produce JSON and XML responses of a Pet Object.
Below is the source code of the spring-mvc-rest-json-xml project.
Code:
package com.simba.demo; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; /** * @author Simba * */ @RestController public class PetController { @RequestMapping(value = "/petDemo", method=RequestMethod.GET, produces= {MediaType.APPLICATION_JSON_VALUE,MediaType.APPLICATION_XML_VALUE}) public ResponseEntity<Pet> getEmployeeById () { Pet pet=new Pet(); pet.setId(1); pet.setPetName("Micky"); pet.setPetType("Rabbit"); pet.setPetOwner("Paul"); return new ResponseEntity(pet,HttpStatus.OK); } }
package com.simba.demo; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * @author Simba * */ @XmlRootElement (name = "pet") @XmlAccessorType(XmlAccessType.NONE) public class Pet implements Serializable { private static final long serialVersionUID = 1L; @XmlAttribute private Integer id; @XmlElement private String petName; @XmlElement private String petType; @XmlElement private String petOwner; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getPetName() { return petName; } public void setPetName(String petName) { this.petName = petName; } public String getPetType() { return petType; } public void setPetType(String petType) { this.petType = petType; } public String getPetOwner() { return petOwner; } public void setPetOwner(String petOwner) { this.petOwner = petOwner; } }
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd" xmlns:mvc="http://www.springframework.org/schema/mvc"> <context:component-scan base-package="com.simba.demo" /> <mvc:annotation-driven /> </beans:beans>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>spring-mvc-rest-json-xml</display-name> <servlet> <servlet-name>rest-api</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:rest-api-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>rest-api</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
<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>spring-mvc-rest-json-xml</groupId> <artifactId>spring-mvc-rest-json-xml</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.7.0</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <artifactId>maven-war-plugin</artifactId> <version>3.0.0</version> <configuration> <warSourceDirectory>WebContent</warSourceDirectory> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.4.1</version> </dependency> </dependencies> </project>
3. Testing
In your eclipse configure tomcat and run your application by selecting Run on server.
When you open the URL: http://localhost:8080/spring-mvc-rest-json-xml/petDemo.json
{ "id": 1, "petName": "Micky", "petType": "Rabbit", "petOwner": "Paul" }
When you open the URL: http://localhost:8080/spring-mvc-rest-json-xml/petDemo.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <pet id="1"> <petName>Micky</petName> <petType>Rabbit</petType> <petOwner>Paul</petOwner> </pet>
OR
You can also test the API by setting Accept header field and without .xml or .json extension in API URL. To get the JSON the Accept header field value will be application/json and for XML the Accept header field value will be application/xml.