The Service component in Spring Boot

In this post we will understand what a Service component is and how it is created in Spring Boot.

What is a Service component

In this other article we learned about the Components and Stereotypes in Spring Boot
We understood that a @Service is a Spring component and stereotype that is part of ‘the service layer’.

A microservice is divided into layers. The service is in the middle layer and contains the business logic.

Spring Boot Controller

A ‘Service’ component is the component in charge of solving the business logic within our application. If we have in our service to manage, for example, a ‘user’ of the system; then the UserService component will be in charge of incorporating the functionalities in its methods.

How do we create the Service component?

What we need to do is to think about the operations we need to perform on our User and create the methods for that purpose. For this example, we will think about the basic operations to create, modify and search that we call CRUD.

CRUD is an acronym that refers to the four minimum functions on an entity –> Create, Read, Update, Delete.

We annotate the class ‘UserService’ as @Service.

@Service
public class UserService {

    private final UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public User create(User user) {
        return userRepository.save(user);
    }

    public User update(User user) {
        return userRepository.save(user);
    }

    public User change(User user) {
        return userRepository.save(user);
    }

    public boolean remove(long id) {
        if (userRepository.existsById(id)) {
            userRepository.deleteById(id);
            return true;
        } else {
            return false;
        }
    }

    public Optional<User> find(long id) {
        return userRepository.findById(id);
    }

}

We see that our service delegates in another component, the Repository, the direct access to the data. The detail of this repository can be seen in another article.

From a controller that we create we call the functionalities of the service.

We start our application to test. Read this article where I explain how to create the main application in Spring Boot and start it.

gradlew booRun
@RestController
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/api/user/{id}")
    public User byId(@PathVariable("id") int id) {
        return userService.find(id).orElseThrow();
    }

    @PostMapping("/api/user/")
    public User create(@RequestBody User user) {
        return userService.create(user);
    }

    @PutMapping("/api/user/")
    public User update(@RequestBody User user) {
        return userService.update(user);
    }

    @PatchMapping("/api/user/")
    public User change(@RequestBody User user) {
        return userService.change(user);
    }

    @DeleteMapping("/api/user/{id}")
    public boolean delete(@PathVariable("id") int id) {
        return userService.remove(id);
    }

}

We create, search and delete a user from the controller that calls the service.

Spring Boot Service from Controller

Conclusion

In this article we learned how to create a Service in SpringBoot, and we understood the purpose of the @Service component in a Rest application.

If you wish to send me any corrections or suggestions, please contact me.
Thanks for your feedback :)

See also