This repo is for testing OpenAPI Generator in Spring Boot. OpenAPI Specification and plugins are used to define endpoints and config to generate automatic code.
Add to your build->plugins
section (default phase is generate-sources
phase)
<!-- OpenAPI Generator Plugin -->
<plugin>
<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>
<!-- RELEASE_VERSION -->
<version>7.4.0</version>
<!-- /RELEASE_VERSION -->
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>${project.basedir}/src/main/resources/api/openapi.yaml</inputSpec>
<generatorName>spring</generatorName>
<ignoreFileOverride>${project.basedir}/src/main/resources/api/.openapi-generator-ignore</ignoreFileOverride>
<configurationFile>${project.basedir}/src/main/resources/api/config.json</configurationFile>
</configuration>
</execution>
</executions>
</plugin>
Add the config.json
file to the path you specified in the plugin configuration (configurationFile
).
{
"library": "spring-boot",
"dateLibrary": "java21",
"useSpringBoot3": "true",
"hideGenerationTimestamp": true,
"sourceFolder": "src/gen/java/main",
"modelPackage": "api.model",
"apiPackage": "api",
"invokerPackage": "api",
"serializableModel": true,
"useTags": true,
"useGzipFeature" : true,
"hateoas": true,
"withXml": true,
"importMappings": {
"ResourceSupport":"org.springframework.hateoas.RepresentationModel",
"Link": "org.springframework.hateoas.Link"
}
}
Add the .openapi-generator-ignore
file to a path specified in the plugin configuration (ignoreFileOverride
).
**/*Controller.java
Enter the following command in your terminal to run the plugin as a stage of the build and generate the code:
mvn clean compile
After finishing the above command, the generated code is placed in target/generated-sources/openapi/src/gen/java/main/api
path.
You can change the generated code path in config.json
.
OpenAPI Generator Maven Plugin