Spring Boot

Spring Boot Rest Example – Restful Webservices CRUD example

REST, or REpresentational State Transfer, is an architectural style to build applications by using APIs that allow the server to communicate with clients using the basic HTTP protocols.

Why REST is popular:
1. It allows the separation between the client and the server.
2. It doesn’t rely on a single technology or programming language.
3. You can build the scalable application or even integrate two different applications using REST APIs
4. REST is stateless and uses basic HTTP methods like GET, PUT, POST, and DELETE, etc. for communication between client and server.

Let’s create a simple spring boot rest example.

More Spring Boot Tutorials

  1. Spring Boot Swagger 2 Example

  2. Spring Boot Hello World Example

  3. Lazy Initialization in Spring Boot 2

For this example I will use below technologies and libraries :
Spring boot 2.1.6.RELEASE
Spring Data JPA
Java 1.8
Spring Web
H2 In-memory database
Maven 3.1.1
Spring Boot DevTools
Spring Boot Test

Spring Boot REST Example

To create the Spring Boot REST program, you have to follow below four steps:
1. Create a new project using the Spring Tool Suite or Spring Initializr Project.
2. Add dependencies in the Maven POM file.
3. Add a controller and expose REST APIs.
4. Add a service layer to communicate with Spring Data JPA’s CrudRepository interface.

Create a new Spring Boot Project

Spring boot rest example
Spring boot rest example
 Spring boot rest example dependencies
Spring boot rest example dependencies

As you could see in the above example, I have created a new project spring-boot-rest-example and added dependencies for Spring Data JPA, H2 Database, and Spring Web Starter.

You can verify those dependencies in pom.xml.

Maven Dependency Tree

Controller to expose REST APIs

For this tutorial, we will CRUD APIs for User Management System.

By using these APIs we can add, retrieve, update, or delete the user details from the database.

To create a User Management System, let’s focus on writing 5 basic APIs

HTTP GET /getAll will return a list of all user details.
HTTP GET /find/{id} will return a user’s detail by an id.
HTTP POST /add is to add a user into the database.
HTTP PUT /update/{id} can be used to update a user based on an id.
HTTP DELETE /delete/{id} can be used to delete a user from the database.

Let’s create a controller (UserController) to expose REST endpoints.

Service layer

The service layer acts as an intermediate layer between a controller and a repository class.

Entity class

Now, let’s create an entity class which is a simple POJO class annotated with JPA annotations.

Entity class also represents a table in the database.

In our case, we will create a User class inside com.codedelay.rest.entity package.

Write the Repository Interface.

UserRepository interface extends CrudRepository.

CrudRepository is a magical interface from Spring Data JPA.

It allows writing simple CRUD functions without writing a single line of code.

Exception handling in Spring Boot REST

A good REST API also covers exception scenarios.

Let discuss one simple scenario.

What will happen if HTTP GET /find/{id} doesn’t find a particular user in the database?

It should throw an exception. Isn’t it?

Let’s add one more class UserNotFoundException class in com.codedelay.rest.exception package.

Now add the Service class to throw the UserNotFoundException exception if there are no user details available in the database for that particular user id.

It is not sufficient to throw java exception.

We have to return some HTTP error when UserNotFoundException occurs.

For this, let’s create a class GlobalExceptionHandler which will return HttpStatus.NOT_FOUND error when UserNotFoundException occurs.

Conclusion

In the above example, we have seen how to create a simple spring boot rest example.

To test this code, let’s add a few dummy data.

Noe test in PostMan to see if the above APIs works or not.

/getAll REST API
/getAll REST API