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.



Final pom.xml

    xmlns:xsi="" xsi:schemaLocation="">
        <!-- xml dependency-->
        <!-- xml dependency end-->


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

public class SpringBootApp {
    public static void main(String[] args) {, args);




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

import com.java4s.model.Customer;

public class SpringJava4sController {

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

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

        return cust;

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; = name; = country;

    public int getCustNo() {
        return custNo;

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

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getCountry() {
        return country;

    public void setCountry(String country) { = country;



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

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.


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


    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.


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.