~~ Spring Boot ~~

 1. Introduction

    1.1 Spring Boot-Intorduction
    1.2 Spring Boot-Architecture
    1.2 Spring Boot-Quick Start

 2. Project Creation

    2.1 Spring Initializer
    2.2 Spring Boot CLI
    2.3 STS IDE
    2.4 Eclipse IDE
    2.5 First Program
    2.6 Spring Boot-Code Structure

 3. Spring Boot – Beans & DI
 4. Spring Boot – Runners
 5. Spring Boot – Properties File
 6. Spring Boot – Looging
 7. Spring Boot – Rest WebServices
 8. Spring Boot – Exception Handling
 9. Spring Boot – Interceptor
 10. Spring Boot – Servlet Filter
 11. Spring Boot – Rest Template
 12. Spring Boot – File HANDLING
 13. Spring Boot – Thymeleaf
 14. Spring Boot – Internationalization
 15. Spring Boot – Scheduling
 16. Spring Boot – Actuator
 17. Spring Boot – Email Handling
 18. Spring Boot – Database Handling
 19. Spring Boot – Caching

Spring Boot | Restful WebServices :

Spring Boot provides very good support for Restful Webservices. In this tutorial we will cover all below important methods with step by step.

  1. GET : To read a resource.
  2. POST : To create a new resource.
  3. PUT : To update an existing resource.
  4. DELETE : To delete the resource.

Before to Start this development, First we will see some useful Annotations which we are going to use here.

@RestController : 

The @RestController is mainly used for to define the Restful Webservice, Below is an example to define this annotation.

@RestController
public class BookController {
}

 

 @RequestMapping :

The @RequestMapping is mainly used for to define the Endpoint URI and Method (get, post, put, delete). Here we need to pass path (URI) and method in this annotation. Below is an example to define this annotation.

@RequestMapping(path = "/getAllBookDetails", method = RequestMethod.GET)
public List getAllBookDetails() {

}

 

@RequestBody :

The @RequestBody annotation is mainly used for to receive the HTTP request body. Below is an example to define this annotation.

public String addBookDetails(@RequestBody List book) {

}

 

 @PathVariable :

The @PathVariable annotation is mainly used for to read an URL template variable. Below is an example to define this annotation.

public List getAllBookDetails(@PathVariable String id) {

}

 

@RequestParam :

The @RequestParam annotation is mainly used for to bind a web request parameter to a method parameter. Below is an example to define this annotation.

public ResponseEntity getBook(
   @RequestParam(value = "name", required = false, defaultValue = "java") String name) {
}

 

Dependencies :

Here we need to add below dependencies in our Project.

   org.springframework.boot
   spring-boot-starter-web    

 

Project Structure :

Below will be our Project Folder Structure.

Restful_Webservices_Project Structure

Here we will create Small Project to read, update, and delete some Book details with help of GET, POST, PUT and DELETE Method.

GET Method : To Fetch All Book Details

The default method is GET Method only, If we are not using any method then by default it will be GET only. This method is mainly use for to read resources. Here Request URI will be “/getAllBookDetails“, which will return all book details.
<?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.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.study4online</groupId>
<artifactId>RestWebservices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>DatabaseHandling-1</name>
<description>Spring_RestWebservices_Demo</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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

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

</project>
package com.study4online;

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

@SpringBootApplication
public class RestWebservicesApplication {

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

}
package com.study4online.model;

public class Book {

private String bookId;
private String bookName;
private String bookDescription;

public Book() {

}

public Book(String bookId, String bookName, String bookDescription) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookDescription = bookDescription;
}

public String getBookId() {
return bookId;
}

public void setBookId(String bookId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public String getBookDescription() {
return bookDescription;
}

public void setBookDescription(String bookDescription) {
this.bookDescription = bookDescription;
}

@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", bookDescription=" + bookDescription + "]";
}

}
package com.study4online.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.study4online.model.Book;
import com.study4online.service.BookService;

@RestController
public class BookController {

@Autowired
BookService bookService;

//Get Method to fetch all Book Details
@RequestMapping(path = "/getAllBookDetails", method = RequestMethod.GET)
public List<Book> getAllBookDetails() {
return bookService.getAllBookDetails();

}

}
package com.study4online.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.study4online.model.Book;

@Service
public class BookService {

Logger logger = LoggerFactory.getLogger(BookService.class);

public static List<Book> blist = new ArrayList<Book>();
static {
blist.add(new Book("Java", "JavaBook", "JavaDescription"));
blist.add(new Book("Tibco", "TibcoBook", "TibcoDescription"));
blist.add(new Book("Spring", "SpringBook", "SpringDescription"));
}

public List<Book> getAllBookDetails() {
return blist;
}

}
GET_Method_getAllBookDetails

GET Method : To Fetch Some Specific Book Detail

Here Request URI will be “/getBookDetails/{id}“, which will return some specific book details. For example if we will pass java instead of id as below, then it will return only java book details.
  1. /getBookDetails/java – It will return java book details only.
  2. /getBookDetails/tibco – It will return tibco book details only.
package com.study4online;

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

@SpringBootApplication
public class RestWebservicesApplication {

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

}
package com.study4online.model;

public class Book {

private String bookId;
private String bookName;
private String bookDescription;

public Book() {

}

public Book(String bookId, String bookName, String bookDescription) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookDescription = bookDescription;
}

public String getBookId() {
return bookId;
}

public void setBookId(String bookId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public String getBookDescription() {
return bookDescription;
}

public void setBookDescription(String bookDescription) {
this.bookDescription = bookDescription;
}

@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", bookDescription=" + bookDescription + "]";
}

}
package com.study4online.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.study4online.model.Book;
import com.study4online.service.BookService;

@RestController
public class BookController {

@Autowired
BookService bookService;

//Get Method To Fetch Specific Book Detail
@RequestMapping(path = "/getBookDetails/{id}", method = RequestMethod.GET)
public List getAllBookDetails(@PathVariable String id) {
return bookService.getBookDetails(id);

}
}
package com.study4online.service;

 

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.study4online.model.Book;

@Service
public class BookService {

Logger logger = LoggerFactory.getLogger(BookService.class);

public static List<Book> blist = new ArrayList<Book>();
static {
blist.add(new Book(“Java”, “JavaBook”, “JavaDescription”));
blist.add(new Book(“Tibco”, “TibcoBook”, “TibcoDescription”));
blist.add(new Book(“Spring”, “SpringBook”, “SpringDescription”));
}

public List<Book> getBookDetails(String id) { List<Book> ublist = new ArrayList<Book>(); for (Book b : blist) { if (b.getBookId().equalsIgnoreCase(id)) { ublist.add(b); } } return ublist; }

}

GET_Method_getBookDetail

POST Method : To Add Book Detail

Here other method we have is POST Method. This method is mainly use for to add resources. Here Request URI will be “/addBookDetails“, which will add book details. Then we will use GET Method with “/getAllBookDetails” to verify our book got added or not.
package com.study4online;

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

@SpringBootApplication
public class RestWebservicesApplication {

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

}
package com.study4online.model;

public class Book {

private String bookId;
private String bookName;
private String bookDescription;

public Book() {

}

public Book(String bookId, String bookName, String bookDescription) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookDescription = bookDescription;
}

public String getBookId() {
return bookId;
}

public void setBookId(String bookId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public String getBookDescription() {
return bookDescription;
}

public void setBookDescription(String bookDescription) {
this.bookDescription = bookDescription;
}

@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", bookDescription=" + bookDescription + "]";
}

}
package com.study4online.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.study4online.model.Book;
import com.study4online.service.BookService;

@RestController
public class BookController {

@Autowired
BookService bookService;

//Get Method To Fetch All Book Details
@RequestMapping(path = "/getAllBookDetails", method = RequestMethod.GET)
public List<Book> getAllBookDetails() {
return bookService.getAllBookDetails();

}

//POST Method To Add New Book
@RequestMapping(path = "/addBookDetails", method = RequestMethod.POST )
public String addBookDetails(@RequestBody List book) {
return bookService.addBookDetails(book);

}

}
package com.study4online.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.study4online.model.Book;

@Service
public class BookService {

Logger logger = LoggerFactory.getLogger(BookService.class);

public static List blist = new ArrayList();
static {
blist.add(new Book("Java", "JavaBook", "JavaDescription"));
blist.add(new Book("Tibco", "TibcoBook", "TibcoDescription"));
blist.add(new Book("Spring", "SpringBook", "SpringDescription"));
}

public List<Book> getAllBookDetails() {
return blist;
}

public String addBookDetails(List book) {
blist.addAll(book);
return "Book Details Added Sucessfully";

}

}
POST_Method_Example
POST_GET_Example

PUT Method : To Update Book Detail

Now other method we have is PUT Method. This method is mainly use for to update resources. Here Request URI will be “/updateBookDetails/{id}“. Here we will pass id as Java and in request will pass JavaScript details, so that it will update Java book details to JavaScript book details. And then finally we will use GET Method with “/getAllBookDetails” to verify our book got updated or not.
package com.study4online;

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

@SpringBootApplication
public class RestWebservicesApplication {

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

}
package com.study4online.model;

public class Book {

private String bookId;
private String bookName;
private String bookDescription;

public Book() {

}

public Book(String bookId, String bookName, String bookDescription) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookDescription = bookDescription;
}

public String getBookId() {
return bookId;
}

public void setBookId(String bookId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public String getBookDescription() {
return bookDescription;
}

public void setBookDescription(String bookDescription) {
this.bookDescription = bookDescription;
}

@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", bookDescription=" + bookDescription + "]";
}

}
package com.study4online.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.study4online.model.Book;
import com.study4online.service.BookService;

@RestController
public class BookController {

@Autowired
BookService bookService;

//Get Method To Fetch All Book Details
@RequestMapping(path = "/getAllBookDetails", method = RequestMethod.GET)
public List<Book> getAllBookDetails() {
return bookService.getAllBookDetails();

}

//PUT Method To Update Book Detail
@RequestMapping(path = "/updateBookDetails/{id}", method = RequestMethod.PUT)
public String updateBookDetails(@PathVariable String id, @RequestBody Book book) {
return bookService.updateBookDetails(id, book);

}

}
package com.study4online.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.study4online.model.Book;

@Service
public class BookService {

Logger logger = LoggerFactory.getLogger(BookService.class);

public static List<Book> blist = new ArrayList<Book>();
static {
blist.add(new Book("Java", "JavaBook", "JavaDescription"));
blist.add(new Book("Tibco", "TibcoBook", "TibcoDescription"));
blist.add(new Book("Spring", "SpringBook", "SpringDescription"));
}

public List<Book> getAllBookDetails() {
return blist;
}

public String updateBookDetails(String id, Book book) {
for (Book b : blist) {
if (b.getBookId().equalsIgnoreCase(id)) {
int index = blist.indexOf(b);
blist.set(index, book);
return "Book Detail Updated Sucessfully.";
}
}
return "Book Detail is not able to updated";
}

}
PUT_method_demo
POST_GET_Example

DELETE Method : To Delete Book Detail

Now Last method we will see DELETE. This method is mainly use for to remove the resources. Here Request URI will be “/deleteBookDetails/{id}“. Here we will pass id as Java and in request. And then finally we will use GET Method with “/getAllBookDetails” to verify our book Java got deleted or not.
package com.study4online;

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

@SpringBootApplication
public class RestWebservicesApplication {

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

}
package com.study4online.model;

public class Book {

private String bookId;
private String bookName;
private String bookDescription;

public Book() {

}

public Book(String bookId, String bookName, String bookDescription) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookDescription = bookDescription;
}

public String getBookId() {
return bookId;
}

public void setBookId(String bookId) {
this.bookId = bookId;
}

public String getBookName() {
return bookName;
}

public void setBookName(String bookName) {
this.bookName = bookName;
}

public String getBookDescription() {
return bookDescription;
}

public void setBookDescription(String bookDescription) {
this.bookDescription = bookDescription;
}

@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", bookDescription=" + bookDescription + "]";
}

}
package com.study4online.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.study4online.model.Book;
import com.study4online.service.BookService;

@RestController
public class BookController {

@Autowired
BookService bookService;

//Get Method To Fetch All Book Details
@RequestMapping(path = "/getAllBookDetails", method = RequestMethod.GET)
public List getAllBookDetails() {
return bookService.getAllBookDetails();

}

//DELETE Method To Remove Book Detail
@RequestMapping(path="/deleteBookDetails/{id}", method = RequestMethod.DELETE)
public String deleteBookDetails(@PathVariable String id) {
return bookService.deleteBookDetails(id);
}

}
package com.study4online.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.study4online.model.Book;

@Service
public class BookService {

Logger logger = LoggerFactory.getLogger(BookService.class);

public static List blist = new ArrayList();
static {
blist.add(new Book("Java", "JavaBook", "JavaDescription"));
blist.add(new Book("Tibco", "TibcoBook", "TibcoDescription"));
blist.add(new Book("Spring", "SpringBook", "SpringDescription"));
}

public List getAllBookDetails() {
return blist;
}

public String deleteBookDetails(String id) {
for(Book b:blist) {
if(b.getBookId().equalsIgnoreCase(id)) {
int index = blist.indexOf(b);
blist.remove(index);
return "Book Detail Removed Sucessfully";
}
}
return "Book Detail Not Able To Remove";
}

}
DELETE_Method_demo
DELETE_GET_MethodDemo

Help Others, Please Share