Spring with Kotorin ―― 7. Service layer

Overview / Description

I've created a server-side application in Kotlin, but it's not yet properly configured. Create a controller class with "Kotorin and Spring Mone-- 2. RestController and Data Class", and the configuration is still such that data is handled directly. Therefore, we will make changes little by little. First, let's consider the "** service **" layer.

service-layer.png

Web applications are roughly divided into the following functions.

--Request / response processing --Processing of application logic --Data processing

Until now, these functions were handled in a single layer. With this configuration, the logic to handle increases and the data model becomes complicated, One class becomes huge and development / maintenance efficiency deteriorates.

Separate each function and divide it into multiple layers.

By the way, the class I was making is RestController, Originally, it is the responsibility to handle request / response. Therefore, the other processing is cut out and it is designated as ** service **.

Assumptions / Environment

Runtime version

Spring Dependencies

Development environment

Procedure / Explanation

Controller class before change

Originally, I was doing things other than request / response processing such as data generation in the controller class as follows.

@RestController
@RequestMapping("/messages")
class SimpleController {

    @GetMapping
    fun getMessages() : List<Message> {
        return listOf(
                Message(
                        UUID.randomUUID().toString(),
                        "First Message",
                        "This is a 1st message on ${getDate()}."
                ),
                Message(UUID.randomUUID().toString(),
                        "Second Message",
                        "This is a 2nd message on ${getDate()}."
                )
        )
    }

    private fun getDate() : String {
        val simpleDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
        return simpleDateFormat.format(Date())
    }
}

Service class

Move the contents originally implemented in the controller class to the service class as follows.

@Service("Message Service")
class MessageService {

    fun getMessages() : List<Message> {
        return listOf(
                Message(
                        UUID.randomUUID().toString(),
                        "First Message",
                        "This is a 1st message on ${getDate()}."
                ),
                Message(UUID.randomUUID().toString(),
                        "Second Message",
                        "This is a 2nd message on ${getDate()}."
                )
        )
    }

    private fun getDate() : String {
        val simpleDateFormat = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
        return simpleDateFormat.format(Date())
    }
}

Annotate the service class with @Service. This makes it available by Dependency Injection in the controller class.

@Autowired
private lateinit var service: MessageService

Use the lateinit keyword to declare an instance without initializing it.

Changed controller class

In the controller class that uses the service class, only the processing related to the request / response should be handled. Make the following changes to call the process implemented in the service class.

@RestController
@RequestMapping("/messages")
class MessageController() {

    @Autowired
    private lateinit var service: MessageService

    @GetMapping
    fun getMessages() = service.getMessages()
}

Summary / Looking back

Developing applications with separate layers for each responsibility is now a standard. Still, in this state, data processing is performed directly from the service layer, so Next, we will separate that part.

This source

Recommended Posts

Spring with Kotorin ―― 7. Service layer
Spring with Kotorin --8 Repository layer
Spring with Kotorin --- 5. Actuator
Spring with Kotorin ―― 1. SPRING INITIALIZR
Spring with Kotorin --8 Repository layer --Supplement: H2 Database
Spring with Kotorin --6 Asynchronous processing
Spring with Kotorin --4 REST API design
Spring with Kotorin --9 Database migration --Flyway
Spring with Kotorin --2 RestController and Data Class
Self-made Validation with Spring
With Kotorin ―― 7. Scoping Function
Download with Spring Boot
Spring with Kotorin --3. Omitting curly braces from the function
A memorandum when creating a REST service with Spring Boot
Generate barcode with Spring Boot
Hello World with Spring Boot
Java Config with Spring MVC
Implement GraphQL with Spring Boot
Get started with Spring boot
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Hello World with Spring Boot!
File upload with Spring Boot
Spring Boot starting with copy
Login function with Spring Security
Using Mapper with Java (Spring)
Spring Boot starting with Docker
Hello World with Spring Boot
Set cookies with Spring Boot
Use Spring JDBC with Spring Boot
Add module with Spring Boot
Getting Started with Spring Boot
Link API with Spring + Vue.js
Implement REST API with Spring Boot and JPA (domain layer)
Create microservices with Spring Boot
Send email with spring boot
Use Basic Authentication with Spring Boot
Implemented authentication function with Spring Security ②
gRPC on Spring Boot with grpc-spring-boot-starter
Implemented authentication function with Spring Security ③
Create an app with Spring Boot 2
Hot deploy with Spring Boot development
Database linkage with doma2 (Spring boot)
Spring Boot programming with VS Code
Until "Hello World" with Spring Boot
Inquiry application creation with Spring Boot
Implemented authentication function with Spring Security ①
Get validation results with Spring Boot
Implement file download with Spring MVC
Oauth2 authentication with Spring Cloud Gateway
(Intellij) Hello World with Spring Boot
Create an app with Spring Boot
Google Cloud Platform with Spring Boot 2.0.0
Check date correlation with Spring Boot
I tried GraphQL with Spring Boot
[Java] LINE integration with Spring Boot
[Spring] Autowired multiple beans. (With bonus)
Beginning with Spring Boot 0. Use Spring CLI
I tried Flyway with Spring Boot
Authentication / authorization with Spring Security & Thymeleaf
Test Spring framework controller with Junit