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:

– 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:

– Add jackson-dataformat-xml:

– Create Customer model:

– Create a simple REST controller with GetMapping and PostMapping

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:

SpringBoot XML REST Service - POST request

REST service’s LOG:


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:

– 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:

III. SourceCode

SpringBootXMLRESTService

By JavaSampleApproach | April 11, 2017.


Related Posts


1 thought on “How to create a SpringBoot XML REST Service”

Got Something To Say:

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

*