Skip to content

Latest commit

 

History

History
81 lines (67 loc) · 2.79 KB

File metadata and controls

81 lines (67 loc) · 2.79 KB

Spring Boot With OpenAPI Generator

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.

Usage (Maven)

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

Generate Code

Enter the following command in your terminal to run the plugin as a stage of the build and generate the code:

mvn clean compile

Result

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.

References

OpenAPI Specification

OpenAPI Generator Maven Plugin

OpenAPI Generator Spring Github

OpenAPI Generator Spring Docs

Modern API Development with Spring and Spring Boot (2021)