Newsletter

Spring Boot – RESTful Web Service with POST Request in XML Example

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

In this article I will am going to show you how to read XML data from REST request using Spring Boot. As I told you in the previous articles, spring boot by default support reading and producing the JSON data. But for any XML support we have to include jackson-dataformat-xml dependency.

xml 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>SpringBootRestfulPostXML</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>

SpringBootRestfulPostXML.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);

    }
}

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;
    }

}

SpringJava4sController.java

package com.java4s.app.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.java4s.model.Customer;

@RestController
public class SpringJava4sController {

    @PostMapping(path = "/save-cust-info")
    public String customerInformation(@RequestBody Customer cust) {

        /* You can call your DAO logic here.
         * For time being I am printing the customer data just to show the POST call is working.
         */

        return "Customer information saved successfully ::." + cust.getCustNo() + " " + cust.getName() + " " + cust.getCountry();
    }
}

application.properties

server.contextPath=/spring-boot-restful-post-xml

Output

Run the application and open http://localhost:8080/spring-boot-restful-post-xml/save-cust-info in Postman.

Request:

<?xml version="1.0" encoding="UTF-8"?>
<customer>
    <country>United States</country>
    <custNo>100</custNo>
    <name>Google</name>
</customer>

Response:

 

​​

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 – RESTful Web Service with POST Request in XML Example”
  1. laxmikant says:

    Very nice explanation, please give more examples.

  2. swathi says:

    Can you please explain spring annotations.

  3. Savani says:

    How to make GET xml request ?

  4. Soubhagya says:

    Hi There,

    I am trying to test the "spring-boot-restful-web-service-with-post-request-in-JSON-example/" through the POST MAN and soapUI but it is throwing below error message:

    {
    "timestamp": "2020-02-10T14:12:34.714+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/spring-boot-restful-post-json/save-cust-info"
    }

    Note: In my application.properties file , i have added the below two lines,

    server.port=8081
    server.servlet.context-path=/spring-boot-restful-post-json

    Please help me out to resolve this issue.

    Thanks in advance!

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.