简体   繁体   English

如何使用 OpenAPITools 创建 Spring-Boot REST 服务器

[英]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.我使用 swagger.io 上的工具创建了一个 Spring Boot 服务器,我现在将其移植到 OpenAPITools。 But I can't find the equivalent generator.但我找不到等效的生成器。 I tried setting the generaterName to spring, but it creates a somewhat different application.我尝试将 generaterName 设置为 spring,但它创建了一个稍微不同的应用程序。 First, it uses a WebMvcConfigurer Bean, even though it's not an MVC application.首先,它使用 WebMvcConfigurer Bean,即使它不是 MVC 应用程序。 Second, the generated Controller and API don't give me an ObjectMapper.其次,生成的控制器和 API 没有给我一个 ObjectMapper。 Third, instead of an HttpServletRequest, they give me a more ambiguous NativeWebRequest instance.第三,它们给了我一个更模糊的 NativeWebRequest 实例,而不是 HttpServletRequest。 Is there a matching generator for the spring REST generator at swagger.io? swagger.io 上是否有与 spring REST 生成器匹配的生成器? Am I doing something wrong?难道我做错了什么?

Here's the openApiTools maven plugin from my pom.xml file:这是我的 pom.xml 文件中的 openApiTools maven 插件:

<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.我知道你在这里发帖的前一天在我们的 Slack 频道上问了同样的问题,但你从未回复我的回复,所以我在这里复制。

Spring should only be outputting the MVC file if you've specified the library as spring-mvc.如果您已将库指定为 spring-mvc,则 Spring 应该只输出 MVC 文件。 Our default library is spring-boot for these generators.对于这些生成器,我们的默认库是spring-boot I'd recommend checking your pom and any external config (specified via configFile or maven properties) to see if you have some conflicting configuration.我建议检查你的 pom 和任何外部配置(通过 configFile 或 maven 属性指定),看看你是否有一些冲突的配置。

You can compare with the config file for this sample in our repo.您可以在我们的 repo 中与此示例的配置文件进行比较。

We support the libraries:我们支持以下库:

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

I did run example you posted above and see spring boot output.我确实运行了您上面发布的示例并查看了 spring boot 输出。 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 .但是,我看到您没有定义输出目录(在您的示例中已注释掉),因此这只会生成为target/generated-sources I'm assuming you maybe could have tried spring-mvc at one point, then began generating into target unexpectedly.我假设您可能曾经尝试过spring-mvc ,然后意外地开始生成target

If you want to generate into your project structure you'll always need to specify output .如果要生成到项目结构中,则始终需要指定output For example, to write out to ./my-springboot , you'd add:例如,要写出./my-springboot ,您需要添加:

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

This outputs springboot code, as expected:这会输出 springboot 代码,正如预期的那样:

➜  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.如果您想生成到子模块中,并且除了排除supportingFiles之外还有一些复杂的逻辑,我之所以提到这个是因为您在示例中对此进行了注释,您可以定义一个外部忽略文件。

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 :假设您不想修改 pom、属性、README 以及您已创建(或计划)的任何实现文件……在您的项目根目录中创建一个名为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.我还建议将生成器更新到5.0.0-beta2版本。

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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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