Spring Boot Rest API — JSON and XML Responces | Code Factory

Reference Link : Link

Donate : Link

pom.xml

<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringBootRest_JsonXml</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootRest_JsonXml</name>
<description>Spring Boot Rest API</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

application.properties

spring.security.user.name=root
spring.security.user.password=root

SpringBootRestJsonXmlApplication.java

package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootRestJsonXmlApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestJsonXmlApplication.class, args);
}
}

RestController.java

package com.example.controller;import java.io.IOException;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import com.example.model.Employee;@org.springframework.web.bind.annotation.RestController
public class RestController {
@GetMapping("/loginPage")
public String main(HttpServletRequest request, HttpServletResponse response) throws IOException {
HttpSession session = request.getSession();
System.out.println(request.getAttribute("_csrf"));
CsrfToken csrfToken = (CsrfToken) request.getAttribute("_csrf");
System.out.println(csrfToken.getToken());
return csrfToken.getToken();
}
@PostMapping(value="/getDataJson", produces="application/json")
public Employee getDataJson(HttpServletRequest request) {
return new Employee("EMP_ID", "EMP_NAME");
}
@PostMapping(value="/getDataXml", produces="application/xml")
public Employee getDataXml(HttpServletRequest request) {
return new Employee("EMP_ID", "EMP_NAME");
}

@PostMapping(value="/getData")
public Employee getData(HttpServletRequest request) {
return new Employee("EMP_ID", "EMP_NAME");
}
}

Employee.java

package com.example.model;import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee {
@XmlElement
private String id;
@XmlElement
private String name;
public Employee() {

}

public Employee(String id, String name) {
this.id = id;
this.name = name;
}

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@Override
public String toString() {
return "Employee [id = " + id + ", name = " + name + "]";
}
}

http://localhost:8080/loginPage without authentication

http://localhost:8080/loginPage with authentication

In Mapping use produces="application/xml" or produces="application/json" for XML or JSON response

pass above _csrf tocken in Body

What if you don’t want to use produces property of Mapping, then how will you receive XML or JSON response?

Answer is, use header while calling any method. Give Key as “Accept” and values as “application/json” if you need JSON response and “application/xml” for Xml response. In this way we can get both json and xml responses from a single method.

Method :
@PostMapping(value="/getData")
public Employee getData(HttpServletRequest request) {
return new Employee("EMP_ID", "EMP_NAME");
}

XML response :

JSON response :

Note : Don’t forgot to pass _csrf tocken in http://localhost:8080/getData URL