I am using openapi-generator-maven-plugin
to generate Feign clients and models from an OpenAPI spec.
Example OpenAPI file:
openapi: 3.0.1
info:
title: ReviewApis
version: 1.0.0
paths:
/create-new-review:
post:
summary: Create a new review
operationId: createNewReview
...
components:
schemas:
Review:
type: object
required: [title, content, score, bookId]
properties:
id:
type: string
format: uuid
Maven plugin config:
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.3.0</version>
<executions>
<execution>
<id>generate-review-service-feign-client</id>
<goals><goal>generate</goal></goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/review-service/review-service.yaml</inputSpec>
<generatorName>spring</generatorName>
<library>spring-cloud</library>
<apiPackage>com.book_reviewers.feign.review_service.api</apiPackage>
<modelPackage>com.book_reviewers.feign.review_service.model</modelPackage>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<useFeignClientUrl>true</useFeignClientUrl>
<apiNameSuffix>Api</apiNameSuffix>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
The FeignClient interfaces are generated correctly, but the models do not have Lombok annotations (@Data
, @Builder
, etc.).
I found this Baeldung article, but it doesn't cover FeignClient generation.
How can I generate both:
using OpenAPI Generator?
I tried:
<configOptions>
<interfaceOnly>true</interfaceOnly>
<useFeignClientUrl>true</useFeignClientUrl>
<apiNameSuffix>Api</apiNameSuffix>
<useLombok>true</useLombok>
<useDataAnnotation>true</useDataAnnotation>
<useBuilderAnnotation>true</useBuilderAnnotation>
<useAllArgsConstructorAnnotation>true</useAllArgsConstructorAnnotation>
<useNoArgsConstructorAnnotation>true</useNoArgsConstructorAnnotation>
</configOptions>
But it didn't help either.
I tried to generate FeignClient and models in separate executions:
<!-- Generate FeignClient interface for Review-Service -->
<execution>
<id>generate-review-service-feign-client</id>
<goals><goal>generate</goal></goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/review-service/review-service.yaml</inputSpec>
<generatorName>spring</generatorName>
<library>spring-cloud</library>
<apiPackage>com.book_reviewers.feign.review_service.api</apiPackage>
<modelPackage>com.book_reviewers.feign.review_service.model</modelPackage>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<useFeignClientUrl>true</useFeignClientUrl>
<apiNameSuffix>Api</apiNameSuffix>
</configOptions>
</configuration>
</execution>
<execution>
<id>generate-review-service-models</id>
<goals><goal>generate</goal></goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/review-service/review-service.yaml</inputSpec>
<generatorName>java</generatorName> <modelPackage>com.book_reviewers.feign.review_service.model</modelPackage>
<configOptions>
<useLombok>true</useLombok>
<useDataAnnotation>true</useDataAnnotation>
<useBuilderAnnotation>true</useBuilderAnnotation>
<useAllArgsConstructorAnnotation>true</useAllArgsConstructorAnnotation>
<useNoArgsConstructorAnnotation>true</useNoArgsConstructorAnnotation>
<generateApiClasses>false</generateApiClasses> <generateSupportingFiles>false</generateSupportingFiles>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
But I get the error:
package okhttp3.logging does not exist
You can use spring generator for both model and interfaces.
Here I had provided updated build plugins for your reference.
<build>
<plugins>
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<version>7.7.0</version>
<executions>
<!-- Your Feign Client Interface generation logic -->
<execution>
<id>generate-review-service-feign-client</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/review-service.yaml</inputSpec>
<generatorName>spring</generatorName>
<library>spring-cloud</library>
<apiPackage>com.book_reviewers.feign.review_service.api</apiPackage>
<modelPackage>com.book_reviewers.feign.review_service.model</modelPackage>
<configOptions>
<interfaceOnly>true</interfaceOnly>
<useFeignClientUrl>true</useFeignClientUrl>
<apiNameSuffix>Api</apiNameSuffix>
<modelDocs>false</modelDocs>
<modelTests>false</modelTests>
</configOptions>
</configuration>
</execution>
<!-- Your Lombok annotated model generation logic -->
<execution>
<id>generate-review-service-models</id>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/review-service.yaml</inputSpec>
<generatorName>spring</generatorName>
<modelPackage>com.book_reviewers.feign.review_service.model</modelPackage>
<configOptions>
<interfaceOnly>false</interfaceOnly>
<generateApis>false</generateApis>
<generateSupportingFiles>false</generateSupportingFiles>
<useLombokAnnotations>true</useLombokAnnotations>
<additionalModelTypeAnnotations>
@lombok.Data
@lombok.NoArgsConstructor
@lombok.AllArgsConstructor
</additionalModelTypeAnnotations>
<modelDocs>false</modelDocs>
<modelTests>false</modelTests>
</configOptions>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
I had added two additional properties for Lombok enablement and Lombok annotations which will going to use.
Below property will inform generator that Lombok is enable for this build command.
<useLombokAnnotations>true</useLombokAnnotations>
Below property will tell generator that these Lombok annotations i want to use on my generated sources.
You can find detailed explanation for same using below additionalModelTypeAnnotations
<additionalModelTypeAnnotations>
@lombok.Data
@lombok.NoArgsConstructor
@lombok.AllArgsConstructor
</additionalModelTypeAnnotations>
And here is the sample generated source for Review.java class.