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

In this post we will create RESTful Web Service with POST Request in XML . If you are new in spring boot then firstly read this post How to create restful web service in spring boot . Now if you want to handle xml request and response in spring boot then you must add jackson-dataformat-xml  dependency in pom.xml .

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

Now we will create UserController.java with restful web service with post request you can read spring boot tutorial step by step spring boot tutorial  and User.java model class and will update pom.xml with jackson-dataformat-xml dependency . Now we will createspring boot rest xml request body example. 

Pom.xml

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<!-- <version>2.2.6.RELEASE</version> -->
		        <version>2.2.2.RELEASE</version>
	<!-- 	<relativePath/> --> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.javavogue</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>demo</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

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

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

DemoApplication.java

package com.javavogue.demo;

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

@SpringBootApplication
public class DemoApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(DemoApplication.class, args);
	}

}

User.java

package com.javavogue.demo.model;

public class User {
	private int userId;
	private String name;
	
	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
	

}

And

UserController.java

package com.javavogue.demo.controller;

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

import com.javavogue.demo.model.User;

@RestController
public class UserController {
	
	
	@PostMapping("/user")
	public User addUser(@RequestBody User user){
		System.out.println("Add user successfully");
		user.setName("name ="+ user.getName());
		return user;
	}
	 
}

Now we will send xml request and got xml response from spring boot application as. Set header as per below .

 

Now send xml request as below

 

 


Notice: compact(): Undefined variable: limits in /home/javaprof/public_html/wp-includes/class-wp-comment-query.php on line 853

Notice: compact(): Undefined variable: groupby in /home/javaprof/public_html/wp-includes/class-wp-comment-query.php on line 853

Leave a Reply

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

− 4 = 4