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"
         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>
    <groupId>spring-boot-example</groupId>
    <artifactId>spring-boot-rest-xml-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <description>spring boot rest xml example or spring boot xml response</description>
    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
       <!-- dependency for xml start-->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>
        <dependency>
            <groupId>org.codehaus.woodstox</groupId>
            <artifactId>woodstox-core-asl</artifactId>
        </dependency>
        <!-- dependency for xml end-->
    </dependencies>
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

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
 */
@RestController
public class DemoXmlController {
    @RequestMapping("/demo")
    public Employee demo() {            // it will return response in xml and json
        Employee employee = new Employee();
        employee.setId(1);
        employee.setName("Java Developer Zone");
        employee.setRole("HR");
        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.
 */
@SpringBootApplication
@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
spring-boot-starter-parent
1.5.4.RELEASE

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 *