Saturday, September 16, 2023
HomeJavaProduce Versions Utilizing OpenAPI With Lombok Annotations

Produce Versions Utilizing OpenAPI With Lombok Annotations

1. Summary

Lombok is a Java collection that assists to minimize boilerplate code like getters, setters, and so on OpenAPI supplies a home to auto-generate a design with Lombok notes.

In this tutorial, we’ll discover exactly how to create a design with Lombok notes utilizing an OpenAPI code generator.

2. Job Arrangement

To start with, allow’s bootstrap a Springtime Boot task and also include the Springtime Boot Beginner Internet and also Lombok dependences:

<< dependence>>.
<< groupId>> org.springframework.boot<.
<< artifactId>> spring-boot-starter-web<.
<< variation>> 3.1.2<.
<< dependence>>.
<< groupId>> org.projectlombok<.
<< artifactId>> lombok<.
<< variation>> 1.18.28<.
<< extent>> supplied<.

Furthermore, we require the Swagger Comments, Gson, and also Java Note API dependences to stop mistakes associated with bundles in the created code:

<< dependence>>.
<< groupId>> javax.annotation<.
<< artifactId>> javax.annotation-api<.
<< variation>> 1.3.2<.

<< dependence>>.
<< groupId>><.
<< artifactId>> gson<.
<< variation>> 2.10.1<.
<< dependence>>.
<< groupId>> io.swagger<.
<< artifactId>> swagger-annotations<.
<< variation>> 1.6.2<.

In the following area, we'll produce an API requirements for a design called Publication and also later on create the code with Lombok comment utilizing the OpenAPI code generator.

3. Getting Version Utilizing OpenAPI

The suggestion of OpenAPI is to compose the API requirements prior to real coding starts Below, we'll produce a requirements documents and also create a design based upon the requirements.

3.1. Developing Version Spec

Initially, allow's produce a brand-new documents called bookapi.yml in the sources folder to specify the Publication requirements:

 openapi: 3.0.2.
variation: 1.0.0.
title: Reserve Shop.
name: MIT.
/ publications:.
- publication.
recap: Obtain All Publications.
summary: effective procedure.
$ ref: '#/ components/schemas/Book'.
summary: Reserve not located.
material: {}
kind: things.
called for:.
- id.
- name.
- writer.
residential properties:.
kind: integer.
style: int64.
kind: string.
kind: string.

In the requirements over, we specify the Publication schema with id, name, and also writer areas. Furthermore, we specify an endpoint to obtain all saved publications.

3.2. Produce a Version With Lombok Note

After specifying the API requirements, allow's include the OpenAPI plugin to the pom.xml to assist create the code based upon the requirements:

<< plugin>>.
<< groupId>> org.openapitools<.
<< artifactId>> openapi-generator-maven-plugin<.
<< variation>> 4.2.3<.
<< implementations>>.
<< implementation>>.
<< objectives>>.
<< objective>> create<.
<< setup>>.
<< inputSpec>>$ {project.basedir}/ src/main/resources/ bookapi.yml<.
<< generatorName>> java<.
<< configOptions>>.
<@lombok.Data @lombok. NoArgsConstructor @lombok. AllArgsConstructor . . < generateApis > incorrect .
<< generateSupportingFiles > incorrect . < generateApiDocumentation > incorrect .
<. . . 

Below, we define the place of>the requirements>declare the plugin to examine additionalModelTypeAnnotations (* )residential property to include 3 Lombok notes to the design. For simpleness, we disable the generation of sustaining data and also API documents.

Ultimately, allow's create the Version by carrying out Expert(* )set up

command:$./ mvnw set up The command over produces a


design in the target folder. 3.3. Generated Code Allow's see the created


design:(* )@lombok. Information @lombok. NoArgsConstructor @lombok. AllArgsConstructor. @javax.
annotation.Generated( worth="org.openapitools.codegen.languages.JavaClientCodegen", day="2023-08-16T09:16:34.322697262 Z").
public course Publication {
public fixed last String SERIALIZED_NAME_ID="id";
@SerializedName( SERIALIZED_NAME_ID).
personal Lengthy id;

public fixed last String SERIALIZED_NAME_NAME="name";
personal String name;

public fixed last String SERIALIZED_NAME_AUTHOR="writer";
personal String writer;
// ...
In the created code over, the 3 Lombok notes we specified in the plugin utilizing the

 additionalModelTypeAnnotations[GMT] residential property are included in the design course.

The @Data comment assists create the getters, setters, and so on, at assemble time. The

@NoArgsConstructor produces a vacant contractor, and also the @AllArgsConstructor produces a fabricator that takes a debate for all areas in the course. 4. Final Thought In this post, we discovered exactly how to create a design with Lombok comment utilizing the OpenAPI code generator. Including the additionalModelTypeAnnotations

residential property supplies us the adaptability to include preferred Lombok notes.

As constantly, the full resource code for the instances is offered over on GitHub


Most Popular

Recent Comments