Spring boot

Spring boot xml response example

Spring Boot using XML response instead of JSON

If your application need to send XML response instead of JSON, Just add Jackson XML extension (jackson-dataformat-xml) on the classpath, Spring boot will automatically render XML responses.

pom.xml

	
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.6.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.candidjava</groupId>
	<artifactId>Spring-Boot-Xml-Response</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>Spring-Boot-Xml-Response Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<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>
	</dependencies>
	<build>
		<finalName>Spring-Boot-Xml-Response</finalName>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>

XmlResponseApplication.java

package com.candidjava;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class XmlResponseApplication {

 public static void main(String[] args) {
  SpringApplication.run(XmlResponseApplication.class, args);
 }

}

StudentController.java

package com.candidjava.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.candidjava.models.Student;
import com.candidjava.service.StudentService;

@RestController
@RequestMapping(value = "/student")
public class StudentController {
 @Autowired
 StudentService service;

 @GetMapping("/getAll")
 public List<Student> get() {
  return service.getAllStudents();
 }

 @GetMapping("/get/{id}")
 public Student getById(@PathVariable("id") String id) {
  return service.getStudentById(id);
 }
}

StudentService.java

package com.candidjava.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;

import com.candidjava.models.Student;

@Service
public class StudentService {

 private List<Student> studentList = new ArrayList<Student> (Arrays.asList(

  new Student("1", "ram", "20"), new Student("2", "arun", "21"), new Student("3", "karthick", "22")
 ));

 public List<Student> getAllStudents() {
  return studentList;
 }

 public Student getStudentById(String id) {
  return studentList.stream().filter(student -> student.getId()
   .equals(id)).findFirst().get();

 }

Student.java

package com.candidjava.models;

public class Student {
 private String id;
 private String name;
 private String age;

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

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getAge() {
  return age;
 }

 public void setAge(String age) {
  this.age = age;
 }

}

Download

Download source code from my github account Click here