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

<?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>
spring.security.user.name=root
spring.security.user.password=root
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);
}
}
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");
}
}
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 + "]";
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store