Implement REST API in Spring Boot

Here's a summary of my personal best practices as of June 2018 regarding the combination of tools when implementing REST APIs in Spring Boot.

Development environment (IDE)

Choices

→ As an IDE, it is used in the official Android development environment, and IntelliJ IDEA is likely to become widespread in the future, but Ultimate Edition is required to use the Spring development support function. Eclipse is a good choice for projects that aren't as good as buying a license.

reference

Build tool

Choices

→ Gradle, which allows you to write processing flexibly with Groovy, is likely to become mainstream in the future. However, Maven is sufficient if it is only used for dependency resolution and build.

reference

Control of HTTP communication (mutual conversion between REST API and Java API)

Choices

→ JAX-RS is a Java standard specification, and the implementation libraries that can be used at the operation level have been enhanced, so it is better to use spring-boot-starter-jersey.

reference

Aspect Oriented Programming (AOP)

Choices

→ It is easy to set Spring AOP with AspectJ annotation, and the setting can be summarized in the code. However, if you want to set the aspect to an instance that is not managed by Spring, or if you want to set it at a timing other than method execution, you need to use AspectJ.

reference

Database access

Choices

→ spring-boot-starter-data-jpa is convenient because simple database operations can be described without SQL.

In addition, it is necessary to install the corresponding connector to connect with each DB. (For example, in the case of MySQL, install mysql-connector-java.)

reference

Connection pool

Choices

→ It is better to use Hikari CP, which has excellent performance and is recommended by Spring Boot.

Transaction management

Choices

→ When using transactions for a single transaction target resource (DB, MQ, etc.), the transaction management function of Spring Framework is sufficient. According to the Spring reference, if you want to use transactions across multiple resources, you should either combine Spring with the JTA function of the application server, or use a transaction library such as Atomikos.

reference

Code generation library

Choices

→ AutoValue is strictly a library for creating Immutable Value Class, but in Lombok, you can use it just by adding an accessor method to a general class, and it can be used in many ways. However, if you want to extend the code generation function, it is better to implement the Extension of AutoValue. (Lombok supports Extension Method on a trial basis, but it is unclear if it will be included in the official version.)

reference

Read configuration file

You can easily read the configuration file by using spring-boot-configuration-processor. There are three ways to read the configuration file.

→ Since settings often have a hierarchical structure, it is easy to use @ConfigurationProperties.

reference

Logging

Choices

→ SLF4J + Logback is likely to become the mainstream in the future. Even if you are using other logging systems, you can aggregate them into SLF4J by the method introduced in Bridging legacy APIs.

reference

test

If there is no problem, it is better to use spring-boot-test. AssertJ (assertion), Hamcrest (match operation), [Mockito](http: / The library required for testing, such as /mockito.org/) (mock), is complete.

reference

Recommended Posts

Implement REST API in Spring Boot
Implement a simple Rest API with Spring Security with Spring Boot 2.0
Implement Spring Boot application in Gradle
Introduce swagger-ui to REST API implemented in Spring Boot
Implement REST API with Spring Boot and JPA (Application Layer)
Implement REST API with Spring Boot and JPA (Infrastructure layer)
Implement REST API with Spring Boot and JPA (domain layer)
Implement a simple Rest API with Spring Security & JWT with Spring Boot 2.0
How to implement optimistic locking in REST API
Set context-param in Spring Boot
Implement GraphQL with Spring Boot
Spring Boot 2 multi-project in Gradle
Major changes in Spring Boot 1.5
NoHttpResponseException in Spring Boot + WireMock
Hello World (REST API) with Apache Camel + Spring Boot 2
[Spring Boot] Get user information with Rest API (beginner)
Customize REST API error response with Spring Boot (Part 2)
Customize REST API error response with Spring Boot (Part 1)
Spring with Kotorin --4 REST API design
Spring Boot Hello World in Eclipse
Spring Boot application development in Eclipse
Write test code in Spring Boot
Spring Boot: Restful API sample project
Implement reCAPTCHA v3 in Java / Spring
spring boot access authorization RESTful API
How to call and use API in Java (Spring Boot)
What is @Autowired in Spring boot?
Thymeleaf usage notes in Spring Boot
Let's find out how to receive in Request Body with REST API of Spring Boot
Proxy and timeout settings when calling API in Spring Rest Template
Change Spring Boot REST API request / response from CamelCase to SankeCase
[Beginner] Let's write REST API of Todo application with Spring Boot
Implement API Gateway Lambda Authorizer in Java Lambda
Launch (old) Spring Boot project in IntelliJ
Build Spring Boot + Docker image in Gradle
Static file access priority in Spring boot
Output Spring Boot log in json format
Local file download memorandum in Spring Boot
Create Java Spring Boot project in IntelliJ
Use DynamoDB query method in Spring Boot
Implement CRUD with Spring Boot + Thymeleaf + MySQL
Implement paging function with Spring Boot + Thymeleaf
DI SessionScope Bean in Spring Boot 2 Filter
Change session timeout time in Spring Boot
Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
SameSite cookie in Spring Boot (Spring Web MVC + Tomcat)
Test controller with Mock MVC in Spring Boot
Asynchronous processing with regular execution in Spring Boot
Run a Spring Boot project in VS Code
Output request and response log in Spring Boot
Try to implement login function with Spring Boot
Use Servlet filter in Spring Boot [Spring Boot 1.x, 2.x compatible]
How to add a classpath in Spring Boot
Java tips-Create a Spring Boot project in Gradle
How to bind to property file in Spring Boot
◆ Get API created by Spring Boot from React
Create a web api server with spring boot
Annotations used in Spring Boot task management tool