How to create a SpringBoot XML REST Service

In the tutorial, JavaSampleApproach will show you way to create a SpringBoot XML REST Service.

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.RELEASE

II. Overview

– If on the classpath has Jackson XML extension (jackson-dataformat-xml), SpringBoot will be used to render XML responses.
– If jackson-dataformat-xml is not available, JAXB in JDK will be used with a required annotation @XmlRootElement in model:


import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {
	private String firstname;
	...
}

– GET requests must have Accept: text/xml header.
– POST requests must have Content-Type: text/xml header.

II. Practice

1. Create Spring Boot project

Create SpringBoot Web App project with dependency spring-boot-starter-web:

...
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
</dependency>
...

– Add jackson-dataformat-xml:

<dependency>
	<groupId>com.fasterxml.jackson.dataformat</groupId>
	<artifactId>jackson-dataformat-xml</artifactId>
</dependency>

– Create Customer model:


package com.javasampleapproach.xmlrestservice.model;

//import javax.xml.bind.annotation.XmlRootElement;

//@XmlRootElement
public class Customer {
	private String firstname;
	private String lastname;
	private int age;
	
	public Customer(){}
	
	public Customer(String firstname, String lastname, int age){
		this.firstname = firstname;
		this.lastname = lastname;
		this.age = age;
	}
	
	public void setFirstname(String firstname){
		this.firstname = firstname;
	}
	
	public String getFirstname(){
		return this.firstname;
	}
	
	public void setLastname(String lastname){
		this.lastname = lastname;
	}
	
	public String getLastname(){
		return this.lastname;
	}
	
	public void setAge(int age){
		this.age = age;
	}
	
	public int getAge(){
		return this.age;
	}
	
	@Override
	public String toString() {
		String info = String.format("Customer Info: firstname = %s, lastname = %s, age = %d", 
												firstname, lastname, age);
		return info;
	}
	
}

– Create a simple REST controller with GetMapping and PostMapping


package com.javasampleapproach.xmlrestservice.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.javasampleapproach.xmlrestservice.model.Customer;

@RestController
public class XMLRestController {
	
	 @GetMapping("/customer")
	 public Customer getCustomer(){
		 return new Customer("Peter", "Smith", 30);
	 }
	 
	 @PostMapping("/customer")
	 public String postCustomer(@RequestBody Customer customer){
		 System.out.println(customer);
		 return "Done";
	 }
	 
}

2. Run and check results

– Build and run the project with Spring Boot App mode.

– Use PostMan to test:
Make a GET request: http://localhost:8080/customer
SpringBoot XML REST Service - GET request

Make a POST request: localhost:8080/customer
with header: Content-Type: text/xml
and Body:

<customer>
    <age>30</age>
    <firstname>Peter</firstname>
    <lastname>Smith</lastname>
</customer>

SpringBoot XML REST Service - POST request

REST service’s LOG:


Customer Info: firstname = Peter, lastname = Smith, age = 30

If don’t use jackson-dataformat-xml, SpringBoot automatically uses JAXB of JDK to convert XML. In the case, We use annotation: @XmlRootElement in model:


import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Customer {
	private String firstname;
	...
}

– Build and Run again:
GET request:
SpringBoot XML REST Service - GET request - no jackson

POST request:
SpringBoot XML REST Service - POST request - no jackson

Note have an addition data in XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

III. SourceCode

SpringBootXMLRESTService



By grokonez | April 11, 2017.

Last updated on May 3, 2021.



Related Posts


3 thoughts on “How to create a SpringBoot XML REST Service”

  1. Hi ,
    I have list of customers.So when I use “Get” method that returns list of customers.
    Even if I use @JacksonXmlRootElement or @XmlRootElement, it is not showing the root element tag what I mentioned or name of the class instead listing like

    ……

    Please help me on this.

    Thanks.

Got Something To Say:

Your email address will not be published. Required fields are marked *

*