1. 概述
Lombok 是一個 Java 庫,可以幫助減少樣板代碼,例如 getter、setter 等。
OpenAPI 提供了一個屬性,可以自動生成帶有 Lombok 註解的模型。
在本教程中,我們將探索如何使用 OpenAPI 代碼生成器生成帶有 Lombok 註解的模型。
2. 項目設置
首先,讓我們創建一個 Spring Boot 項目,並添加 Spring Boot Web Starter 和 Lombok 依賴項:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>3.1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<scope>provided</scope>
</dependency>
此外,我們需要 Swagger 標註、Gson 和 Java 標註 API 依賴項,以防止生成代碼中包相關的錯誤:
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>io.swagger.core.v3</groupId>
<artifactId>swagger-annotations</artifactId>
<version>2.2.19</version>
</dependency>
<dependency>
<groupId>org.openapitools</groupId>
<artifactId>jackson-databind-nullable</artifactId>
<version>7.1.0</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.1.0</version>
</dependency>
在下一部分,我們將為名為 Book 的模型創建一個 API 規範,並稍後使用 OpenAPI 代碼生成器生成代碼,使用 Lombok 註解。
3. Generating Model Using OpenAPI
The idea of OpenAPI is to write the API specification before actual coding begins.
Here, we’ll create a specification file and generate a model based on the specification.
3.1. Creating Model Specification
First, let’s create a new file named bookapi.yml in the resources folder to define the Book specification:
openapi: 3.0.2
info:
version: 1.0.0
title: Book Store
license:
name: MIT
paths:
/books:
get:
tags:
- book
summary: Get All Books
responses:
200:
description: successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/Book'
404:
description: Book not found
content: { }
components:
schemas:
Book:
type: object
required:
- id
- name
- author
properties:
id:
type: integer
format: int64
name:
type: string
author:
type: string
In the specification above, we define the Book schema with id, name, and author fields. Additionally, we define an endpoint to get all stored books.
3.2. Generate a Model With Lombok Annotation
After defining the API specification, let’s add the OpenAPI plugin to the pom.xml to help generate the code based on the specification:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.7.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/bookapi.yml</inputSpec>
<generatorName>spring</generatorName>
<configOptions>
<additionalModelTypeAnnotations>@lombok.Data @lombok.NoArgsConstructor @lombok.AllArgsConstructor</additionalModelTypeAnnotations>
</configOptions>
<generateApis>false</generateApis>
<generateSupportingFiles>false</generateSupportingFiles>
<generateApiDocumentation>false</generateApiDocumentation>
<configOptions>
<useJakartaEe>true</useJakartaEe>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
Here, we specify the location of the specification file for the plugin to check during the generation process. Also, we add the additionalModelTypeAnnotations property to add three Lombok annotations to the model.
For simplicity, we disable the generation of supporting files and API documentation.
Finally, let’s generate the Model by executing Maven install command:
$ ./mvnw install
The command above generates a Book model in the target folder.
3.3. Generated Code
Let’s see the generated Book model:
@lombok.Data @lombok.NoArgsConstructor @lombok.AllArgsConstructor
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2024-01-03T22:22:14.557819500+02:00[Europe/Bucharest]")
public class Book {
private Long id;
private String name;
private String author;
// ..
}
In the generated code above, the three Lombok annotations we defined in the plugin using the additionalModelTypeAnnotations property are added to the model class.
The @Data annotation helps generate the getters, setters, etc., at compile time. The @NoArgsConstructor generates an empty constructor, and the @AllArgsConstructor generates a constructor that takes an argument for all fields in the class.
4. 結論
在本文中,我們學習瞭如何使用 OpenAPI 代碼生成器生成帶有 Lombok 註解的模型。添加 additionalModelTypeAnnotations 屬性使我們能夠添加所需的 Lombok 註解。