Spring Boot Rest XML CRUD example – Web service with XML Request and Response

Hello everyone, Today we will learn how to develop Web service with XML Request and Response in Spring Boot. You can clone the complete source code from our Github Repository.

Technologies used :     

  • Spring Boot 2.5.4   
  • Java 11
  • Spring Data JPA
  • H2DB
  • Jackson Dataformat XML 2.10.1 
  • Maven 3  

Project Structure:

– WebConfig implements WebMvcConfigurer. This is where we set the default content type.
– Book data model class corresponds to entity and table books.
– BookRepository is an interface that extends JpaRepository for CRUD methods and custom finder methods. It will be autowired in BookController.
– BookController is a RestController which has request mapping methods for RESTful requests such as: getBookById, createBook, updateBook, deleteBook...
– Configuration for Spring Datasource, JPA & Hibernate in application.properties.
– pom.xml contains dependencies for Spring Boot, Jackson Dataformat XML, JPA & H2DB.

2)Maven/Dependency Management [pom.xml] 

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"  
<relativePath/> <!-- lookup parent from repository -->
<description>Demo project for Spring Boot</description>



Rest Controller

package com.knf.springbootrestxml.controller;

import com.knf.springbootrestxml.model.Book;
import com.knf.springbootrestxml.repository.BookRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@CrossOrigin(origins = "http://localhost:8080")
public class BookController {

BookRepository bookRepository;

public ResponseEntity<List<Book>> getAllBooks(@RequestParam(required = false)  
                 String title) {
try {
List<Book> books = new ArrayList<Book>();

if (title == null)
else {

if (books.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);

return new ResponseEntity<>(books, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);

public ResponseEntity<Book> getBookById(@PathVariable("id") long id) {
Optional<Book> bookData = bookRepository.findById(id);

if (bookData.isPresent()) {
return new ResponseEntity<>(bookData.get(), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);

public ResponseEntity<Book> createBook(@RequestBody Book _book) {
try {
Book book = bookRepository.save(new Book(_book.getTitle(),
                             _book.getDescription(), false));
return new ResponseEntity<>(book, HttpStatus.CREATED);
} catch (Exception e) {
return new ResponseEntity<>(null, HttpStatus.EXPECTATION_FAILED);

public ResponseEntity<Book> updateBook(@PathVariable("id") long id,
                               @RequestBody Book book) {
Optional<Book> bookData = bookRepository.findById(id);

if (bookData.isPresent()) {
Book _book = bookData.get();
return new ResponseEntity<>(bookRepository.save(_book), HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);

public ResponseEntity<HttpStatus> deleteBook(@PathVariable("id") long id) {
try {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (Exception e) {
return new ResponseEntity<>(HttpStatus.EXPECTATION_FAILED);


Modal class(Book.java)

package com.knf.springbootrestxml.model;

import javax.persistence.*;

@Table(name = "books")
public class Book {

@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Column(name = "title")
private String title;

@Column(name = "description")
private String description;

@Column(name = "isPublished")
private boolean published;

public Book() {

public Book(String title, String description, boolean published) {
this.title = title;
this.description = description;
this.published = published;

public long getId() {
return id;

public void setId(long id) {
this.id = id;

public String getTitle() {
return title;

public void setTitle(String title) {
this.title = title;

public String getDescription() {
return description;

public void setDescription(String description) {
this.description = description;

public boolean isPublished() {
return published;

public void setPublished(boolean published) {
this.published = published;

public String toString() {
return "Book{" +
"id=" + id +
", title='" + title + '\'' +
", description='" + description + '\'' +
", published=" + published +


Repository Layer(BookRepository)

package com.knf.springbootrestxml.repository;
import java.util.List;
import com.knf.springbootrestxml.model.Book;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BookRepository extends JpaRepository<Book, Long> {
List<Book> findByTitleContaining(String title);



package com.knf.springbootrestxml.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

public class WebConfig implements WebMvcConfigurer {
public void configureContentNegotiation(final 
                 ContentNegotiationConfigurer configurer) {


Driver Class(SpringbootrestxmlApplication) 

package com.knf.springbootrestxml;

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

public class SpringbootrestxmlApplication {

public static void main(String[] args) {
SpringApplication.run(SpringbootrestxmlApplication.class, args);




$ mvn spring-boot:run


Open POSTMAN tool, select request type, specify the URI

1)Create Book

2)Update Book

3)Get Book by Id

4)Delete Book

More related topics,

Java - Angular - VueJS - ReactJS

NodeJS - Angular - VueJS - ReactJS

Python - Flask  - Angular - VueJS - ReactJS

Popular posts from this blog

Spring boot video streaming example-HTML5

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Java - Blowfish Encryption and decryption Example