Code Monkey home page Code Monkey logo

apifirst-codegen's Introduction

FirstApi approach with OpenAPI generator plugin and SpringDoc OpenAPI UI to your Spring Boot Project

Tested with Java 17 and SpringBoot 2.7.7

STEP 1

Add the following property version for openapitools in pom.xml:

<properties>
    <openapitools.generator.version>6.4.0</openapitools.generator.version>
</properties>

Add the following dependencies:

    <!--OPENAPI GENERATOR MAVEN PLUGIN-->
    <dependency>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>${openapitools.generator.version}</version>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>org.openapitools</groupId>
        <artifactId>jackson-databind-nullable</artifactId>
        <version>0.2.4</version>
    </dependency>
    
    <!--SPRINGDOC OPENAPI-->
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.6.13</version>
    </dependency>

STEP 2

Generate your api.yaml file
You can use https://editor.swagger.io/
Generate a file called api.yaml and place it in src/main/resources

STEP 3

Add the following plugging in your pom.xml file

    <!--OpenApi Codegen-->
    <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>
        <version>${openapitools.generator.version}</version>
        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
                    <generatorName>spring</generatorName>
                    <generateApiTests>false</generateApiTests>
                    <generateModelTests>false</generateModelTests>
                    <modelNameSuffix>Dto</modelNameSuffix>
                    <configOptions>
                        <sourceFolder>src/gen/java/main</sourceFolder>
                        <oas3>true</oas3>
                        <useSpringController>true</useSpringController>
                        <useSpringfox>false</useSpringfox>
                        <interfaceOnly>true</interfaceOnly>
                    </configOptions>
                </configuration>
            </execution>
        </executions>
    </plugin>

STEP 4

Add the following property in application.properties file
This is optional but could avoid few future problems


spring.mvc.pathmatch.matching-strategy=ant-path-matcher

STEP 5

Implement the generated methods
To complete your API, you can implement the generated methods by the codegen plugging on the default path:
target/generated_sources/openapi/src/gen/java

STEP 6

Check Swagger UI website
Run your application and access the following url to check your API documentation:


host:port/swagger-ui/index.html
http://localhost:8080/swagger-ui/index.html

http://localhost:8080/swagger-ui/index.html

apifirst-codegen's People

Contributors

victormartingil avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.