Newsletter

Spring Boot – Example of RESTful Web Service with XML Response

Spring-Boot-Tutorials » on May 23, 2018 { 4 Comments } By Sivateja

Spring boot services by default gives the response in JSON format, but we can reverse this functionality in such a way that the default response will be in XML.  In order to do that we have to add a new dependency called jackson-dataformat-xml.  With this dependency services by default gives the response in XML format and if you want to see the response in JSON, just append .json to the URL that’s it 🙂 I will show you with an example.

Dependency

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

Final 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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.java4s</groupId>
    <artifactId>SpringBootRestfulXML</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- xml dependency-->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
        </dependency>
        <!-- xml dependency end-->
    </dependencies>
    <properties>
        <java.version>1.8</java.version>
    </properties>
</project>

SpringBootApp.java

package com.java4s.app;

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

@SpringBootApplication
public class SpringBootApp {
    public static void main(String[] args) {

        SpringApplication.run(SpringBootApp.class, args);

    }
}

application.properties

server.contextPath=/springbootrestxml

SpringJava4sController.java

package com.java4s.app.controller;

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

import com.java4s.model.Customer;

@RestController
public class SpringJava4sController {

    @GetMapping(path = "/get-cust-info")
    public Customer customerInformation() {
        Customer cust = new Customer();

        cust.setCustNo(100);
        cust.setName("Bank of America");
        cust.setCountry("United States");

        return cust;
    }
}

Customer.java

package com.java4s.model;

public class Customer {

    private int custNo;
    private String name;
    private String country;

    public Customer() {

    }

    public Customer(int custNumber, String name, String country) {
        this.custNo = custNumber;
        this.name = name;
        this.country = country;
    }

    public int getCustNo() {
        return custNo;
    }

    public void setCustNo(int custNo) {
        this.custNo = custNo;
    }

    public String getName() {
        return name;
    }

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

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

}

Output

Run the application and hit http://localhost:8080/springbootrestxml/get-cust-info

If we want to see the response in JSON, append .json to the URL.. http://localhost:8080/springbootrestxml/get-cust-info.json

 

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

4 Responses to “Spring Boot – Example of RESTful Web Service with XML Response”
  1. arrchana says:

    Hi,

    Your blog is really good and useful. I'm learning the spring by using your blog only. I'm trying a task like read data from excel and stored into pojo whenever I hit a spring boot based REST API. Is that possible. give me some idea on that.

  2. Rajeswari says:

    .json url is getting status 404..not giving json response

  3. Ajay says:

    .json is not giving json response.

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.