1. Overview

During spring boot application development XML response also need to requires at some places.  The controller should return XML response by spring boot rest services. Spring boot provides easy configuration to convert bean result to XML response. jackson-dataformat-xml must be required in classpath to achieve this functionality. Need to add a required dependency in pom.xml file which has been mention in below example.

Spring boot default configuration is URL based configuration so request ends with like .xml than it will convert the response to XML and .json than it will convert the response to JSON. The same controller behaves different ways based on URL.

2. Example

Spring boot rest xml example

Spring boot rest XML example

2.1 pom.xml

add jackson-dataformat-xml and woodstox-core-asl dependency for XML response.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <description>spring boot rest xml example or spring boot xml response</description>
    <!-- Inherit defaults from Spring Boot -->
       <!-- dependency for xml start-->
        <!-- dependency for xml end-->
    <!-- Package as an executable jar -->

2.2 DemoXmlController

@RestController indicate that it’s rest API. We can also use @RequestMapping + @ResponseBody for an individual controller.

package com.javadeveloperzone.controller;
import com.javadeveloperzone.model.Employee;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 * Created by Lenovo on 19-07-2017.
 * This is spring boot rest xml example or spring boot xml response example
public class DemoXmlController {
    public Employee demo() {            // it will return response in xml and json
        Employee employee = new Employee();
        employee.setName("Java Developer Zone");
        return employee;

2.3 SpringBootConfig

package com.javadeveloperzone;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
 * Created by JavaDeveloperZone on 19-07-2017.
@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it will start application

2.5 Employee

package com.javadeveloperzone.model;
 * Created by Java Developer Zone on 29-08-2017.
public class Employee {
    private int id;
    private String name;
    private String role;
    public int getId() {
        return id;
    public void setId(int id) {
        this.id = id;
    public String getName() {
        return name;
    public void setName(String name) {
        this.name = name;
    public String getRole() {
        return role;
    public void setRole(String role) {
        this.role = role;

2.6 Demo

  1. http://localhost:8080/demo.xml

It will convert the response to XML and return XML response:

spring boot rest xml example

spring boot rest xml example

2. http://localhost:8080/demo.json

It will convert the response to JSON and return JSON response:

spring boot rest json example

spring boot rest JSON example

3. References

4. Source Code

Spring boot Rest XML example.zip

Git : https://github.com/subhashlamba/spring-examples/tree/master/spring-boot-xml-rest-example

Was this post helpful?

3 comments. Leave new

How did you get woodstox-core-asl dependency to work without specifying the version? It says here: https://docs.spring.io/platform/docs/current-SNAPSHOT/reference/htmlsingle/ (Part VI. Appendices) that adding the version is optional, but it doesn’t seem to work for me. Thanks!

It will based on parent properties in maven. org.springframework.boot

Still doesn’t work. Can you give me the exact path in the dependency tree? Everywhere I googled, the version is specified.

Leave a Reply

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