Skip to main content
spring-boot logo

Spring Boot REST web service – Part 2 – CRUD operations, Service Layers, Assemblers and Utility classes

spring-boot logo

We already implemented simple REST web service in the previous tutorial.

Building a simple REST Service with Spring Boot

Spring Boot REST web service – Part 1 – Spring Data and MySQL

In this tutorial, I am going to implement new functionalities on top of the previous tutorial. Mainly, focusing on these following areas.

  • CRUD Operations
  • Service Layers
  • Assemblers & Value Objects (VO)
  • Utility classes

CRUD Operations

CRUD stands for Create, Read, Update and Delete. Actually, these are basic functions in most of the applications. As you remember, we created the User table in the previous tutorial. Create a new user, update existing user, find a user by user-id and delete user are CRUD operations in the User table.

Service Layers

Do you remember the UserRepository interface in the previous tutorial? Basically, it handles all of the operations that related to the User table. When you building an application, you need to call no of database request to fulfill a task. Let say, you want to create an invoice. You need to get customer details, order details, item details, calculate a total, calculate a taxes, etc. According to the example, it has to call the CustomerRepository, OrderRepository, ItemRepository. Where do we place these business logics? in Controller. Yes, we can place in Controllers. But it might be increased the code complexity and difficult to maintain when growing requirements. So that we can introduce a Service Layer. It can be contained business logics.

This is an example for an Invoicing Service. You can see, it is connected with few of repositories. It might be connected with another service. Let say, an application has 30 repositories. But it might have 4 or 5 service layers. Another example is, an UserService might be connected with UserRepository, OrganizationRepository and UserRoleRepository.

Now, We are going to implement the UserService for our application. Create a new package com.mydevgeek.service and create an UserService.java interface and UserServiceImpl.java class.

Assemblers and Value Objects (VO)

The Assemblers are used to convert one model to another model. For an example, get the User model. It has no of properties such as first name, last name, created date & time, updated date and time, etc. Give all information about the user to outside the world, is not a good practice. So that, we have to modify the User model to another model. Basically, the idea behind the concept is outside world should not be known about internal models.

Assemblers – we can call it “Transformers” too. Anyway, there is no any naming convention. You can use “Assemblers”, “Transformers” or any suitable name. Their responsibility is some model is converted to another.

Value Objects – Actually, it is simple POJO class (properties and their getters and setters). According to this tutorial, we already have a User class. But we can not pass the same User class to the outside. So that we will have to create another class. We can name it like this UserVO, CreateUserVO, UpdateVO. Keep remember, there is no any naming convention. Or you can name it such as CreateUserRequest, ViewUserResponse, etc.

Now, we’ll create User VOs and the User Assembler class.

We need to 3 User VOs

  • CreateUserVO – use for getting user info when creating it.
  • UpdateUserVO – use for getting user info when updating it.
  • UserVO – use for returning user info.

Create a package com.mydevgeek.vo.

 

Then create another package com.mydevgeek.assemblers.

@Component -(Spring Documentation) This is a general-purpose stereotype annotation indicating that the class is spring component. This will be created a Singleton instance. That’s suitable for this task as well.

If you want to create a new class instance each time one is needed, use this annotation with @Component.

I think, now you have an idea about the responsibility of Assembler classes.

Another important thing is, in Assembler, we can call another Services or Repositories like what we are doing in Controllers.

Utility Classes

A utility class is a class that uses for define common and reusable methods.

Create a package com.mydevgeek.util and create a utility class that’s name is UserUtil.java.

  • Create private constructor to avoid create multiple instance.
  • All methods must be static.
  • Should not call other Services, Assemblers, Controllers, Repositories.

 

Controllers

Now we’re going to create a controller class that uses to connect with outside. Create a package com.mydevgeek.controller. Create a UserController.java class.

Best practices

  • Use RequestMethod.GET to get some data.
  • Use RequestMethod.POST to create new data.
  • Use RequestMethod.PUT to modify data.
  • Use RequestMethod.DELETE to remove data.
  • Finally, as a practice, don’t directly call with the Repository. Every time call through the Service Layer.

Run and Test it

As mentioned in Part-1, you can run using command line or intelliJ.

Create a User using POSTMAN.

create a user by postman

 

Modify a User

modify a user by postman

 

Get a User by ID

get a user by postman

 

Delete a User

delete a user by postman

 

Project Code :- GitHub

Next tutorial will be Validation and Exception Handling.


4305total visits,15visits today

  • Pingback: Spring Boot REST web service – Part 1 – Spring Data and MySQL | MyDevGeek()

  • Jorge Reyes-Spindola

    Thank you very much for a very enlightening tutorial. It helped me quite a bit.

    • mydevgeek

      Thanks!!

  • eli

    WOW. Among the best tutorials i have ever read. It really shed the much needed light i needed.
    I had a question. How can i send data from a form in a web application(made with spring boot), to an api, then the database.
    Thanks alot.
    Keep them comming.
    Eli

    • mydevgeek

      Thanks, you have to call REST service through your web application. I’ll write a tutorial about that using Angular 4 and Spring boot.

  • Hi Damith, thank you for sharing great content 🙂

    Few questions:

    1. Are there any benefits of making assembler as Spring component over simple utility class with static methods?

    2. Shouldn’t assembler be injected and used in service? For example if controller needs to return more complex object that consists from several entities…

    Thanks!

    • mydevgeek

      Hi, Thanks your comment.
      1. Main advantage is, you can easily mock it. Normally, we’re using utility methods as a part of complex method. If we use it as a Spring component, we will be able to mock and test the complex method.
      2. Yes, definitely you can use it. There is no defined way to do that. you can use it in service, controller or both.

    • akhmadGuntar

      Yes, this is a great content. The series are great, short yet profound. and this concept of assembler and value object is very interesting!

RSS
Follow by Email
Facebook
Facebook
Google+
http://mydevgeek.com/spring-boot-rest-web-service-part-2-crud-operations-service-layers-assemblers-utility-classes
Twitter