简体   繁体   中英

How to Create a Spring-Boot REST Server Using OpenAPITools

I created a Spring Boot server using the tools at swagger.io, which I'm now porting to OpenAPITools. But I can't find the equivalent generator. I tried setting the generaterName to spring, but it creates a somewhat different application. First, it uses a WebMvcConfigurer Bean, even though it's not an MVC application. Second, the generated Controller and API don't give me an ObjectMapper. Third, instead of an HttpServletRequest, they give me a more ambiguous NativeWebRequest instance. Is there a matching generator for the spring REST generator at swagger.io? Am I doing something wrong?

Here's the openApiTools maven plugin from my pom.xml file:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <!-- RELEASE_VERSION -->
    <version>4.3.1</version>
    <!-- /RELEASE_VERSION -->
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <!-- Configuration properties taken from -->
                <!-- https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator-maven-plugin/README.md -->
                <inputSpec>${project.basedir}/src/gen/java/main/pizzeria.yaml</inputSpec>
                <generatorName>spring</generatorName>
                <!-- <output>${project.basedir}</output>-->
                <!-- Defaults to ${project.build.directory}/generated-sources/openapi -->
                <apiPackage>com.dummy.pizzeria.api</apiPackage>
                <modelPackage>com.dummy.pizzeria.model</modelPackage>
                <invokerPackage>com.dummy.pizzeria</invokerPackage>
                <packageName>com.dummy.pizzeria.objects</packageName>
                <groupId>neptunedreams</groupId>
                <artifactId>pizzeria</artifactId>
                <library>spring-boot</library>
                <generateModelTests>false</generateModelTests>
                <!--<generateSupportingFiles>false</generateSupportingFiles>-->
                <configOptions>
                    <!-- configOptions are specific to the spring generator. These are taken from -->
                    <!-- https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/spring.md -->
                    <sourceFolder>gen</sourceFolder>
                    <bigDecimalAsString>true</bigDecimalAsString>
                    <dateLibrary>java8</dateLibrary> <!-- Default-->
                    <performBeanValidation>true</performBeanValidation>
                    <useBeanValidation>true</useBeanValidation>
                    <skipDefaultInterface>true</skipDefaultInterface>
                    <library>spring-boot</library>
                    <interfaceOnly>false</interfaceOnly>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>

I know you asked this same question on our Slack channel the day before you posted here, but you never replied to my response so I'm duplicating it here.

Spring should only be outputting the MVC file if you've specified the library as spring-mvc. Our default library is spring-boot for these generators. I'd recommend checking your pom and any external config (specified via configFile or maven properties) to see if you have some conflicting configuration.

You can compare with the config file for this sample in our repo.

We support the libraries:

  • spring-mvc
  • spring-boot
  • spring-cloud

I did run example you posted above and see spring boot output. However, I see that you're not defining the output directory (it's commented out in your example) so this would just generate into target/generated-sources . I'm assuming you maybe could have tried spring-mvc at one point, then began generating into target unexpectedly.

If you want to generate into your project structure you'll always need to specify output . For example, to write out to ./my-springboot , you'd add:

<output>${project.basedir}/my-springboot</output>

This outputs springboot code, as expected:

➜  examples git:(master) ✗ tree my-springboot
my-springboot
├── README.md
├── pom.xml
└── src
    └── main
        ├── java
        │   ├── com
        │   │   └── dummy
        │   │       └── pizzeria
        │   │           ├── OpenAPI2SpringBoot.java
        │   │           ├── RFC3339DateFormat.java
        │   │           ├── api
        │   │           │   ├── ApiUtil.java
        │   │           │   ├── PetApi.java
        │   │           │   ├── PetApiController.java
        │   │           │   ├── StoreApi.java
        │   │           │   ├── StoreApiController.java
        │   │           │   ├── UserApi.java
        │   │           │   └── UserApiController.java
        │   │           └── model
        │   │               ├── Category.java
        │   │               ├── ModelApiResponse.java
        │   │               ├── Order.java
        │   │               ├── Pet.java
        │   │               ├── Tag.java
        │   │               └── User.java
        │   └── org
        │       └── openapitools
        │           └── configuration
        │               ├── HomeController.java
        │               └── OpenAPIDocumentationConfig.java
        └── resources
            └── application.properties

12 directories, 20 files

If you want to generate into a sub-module and you have some complex logic aside from excluding supportingFiles , which I mention because you've commented this out in your example, you can define an external ignore file.

Suppose you don't want to modify the pom, properties, README, and any implementation files you have created (or plan to)… create a file in your project root called my-springboot.ignore :

# This file works like .gitignore, patterns here won't be written or overwritten.
# Test files are never overwritten.
**/pom.xml
**/application.properties
**/README.md
**/*Impl.java

I'd also recommend updating to version 5.0.0-beta2 of the generator.

Full code

Here's the full plugin node I used to test locally, including all recommendations above.

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>5.0.0-beta2</version>
    <executions>
    <execution>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <ignoreFileOverride>${project.basedir}/my-springboot.ignores</ignoreFileOverride>
            <inputSpec>${project.basedir}/openapi.yaml</inputSpec>
            <generatorName>spring</generatorName>
            <apiPackage>com.dummy.pizzeria.api</apiPackage>
            <modelPackage>com.dummy.pizzeria.model</modelPackage>
            <invokerPackage>com.dummy.pizzeria</invokerPackage>
            <packageName>com.dummy.pizzeria.objects</packageName>
            <groupId>neptunedreams</groupId>
            <artifactId>pizzeria</artifactId>
            <library>spring-boot</library>
            <generateModelTests>false</generateModelTests>
            <output>${project.basedir}/my-springboot</output>
            <configOptions>
                <bigDecimalAsString>true</bigDecimalAsString>
                <dateLibrary>java8</dateLibrary>
                <performBeanValidation>true</performBeanValidation>
                <useBeanValidation>true</useBeanValidation>
                <skipDefaultInterface>true</skipDefaultInterface>
                <library>spring-boot</library>
                <interfaceOnly>false</interfaceOnly>
            </configOptions>
        </configuration>
    </execution>
    </executions>
</plugin>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM