Skip to content

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.

License

Notifications You must be signed in to change notification settings

mehdizebhi/spring-boot-openapi-codegen-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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)

About

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.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages