Spring Boot – Example of RESTful Web Service with XML

In Previous post we have create spring boot restful web services json example .  Now we will create spring boot restful webservice with xml .  For this we need to add jackson-dataformat-xml dependency in pom file.

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

Note -> After add this dependency we will pass Accept header as application/xml then will get response in xml if we pass application/json then got response in json. 

Firstly we will make a get restful web service in spring boot for  this we will create UserController.java , one model User.java and add jackson-dataformat-xml dependency in pom.xml as below.

 

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

}

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 {
	
	@GetMapping("/user")
	public User getUser(){
		User user = new  User();
		user.setName("Deepak");
		user.setUserId(10);
		return user;
	}
	
	
	 
}

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

}

Now run spring boot application and hit api in postman as

 

 

 

 

spring boot restful webservice with xml response
spring boot restful webservice with xml response
spring boot restful webservice with json response
spring boot restful web service with json response 

See full example How to create restful webservice in spring boot click here . Hope you enjoy this post.

Leave a Reply

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

+ 32 = 38