如何使用 OpenAPITools 创建 Spring-Boot REST 服务器 [英] How to Create a Spring-Boot REST Server Using OpenAPITools

查看:54
本文介绍了如何使用 OpenAPITools 创建 Spring-Boot REST 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 swagger.io 上的工具创建了一个 Spring Boot 服务器,我现在将其移植到 OpenAPITools.但我找不到等效的生成器.我尝试将 generaterName 设置为 spring,但它创建了一个稍微不同的应用程序.首先,它使用 WebMvcConfigurer Bean,即使它不是 MVC 应用程序.其次,生成的控制器和 API 没有给我一个 ObjectMapper.第三,它们给了我一个更模糊的 NativeWebRequest 实例,而不是 HttpServletRequest.swagger.io 上是否有与 spring REST 生成器匹配的生成器?我做错了什么吗?

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?

这是我的 pom.xml 文件中的 openApiTools maven 插件:

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>

推荐答案

我知道你在这里发帖的前一天在我们的 Slack 频道上问了同样的问题,但你从未回复我的回复,所以我在这里复制.

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-mvc,Spring 应该只输出 MVC 文件.对于这些生成器,我们的默认库是 spring-boot.我建议检查你的 pom 和任何外部配置(通过 configFile 或 maven 属性指定),看看你是否有一些冲突的配置.

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.

我们支持库:

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

我确实运行了您在上面发布的示例并查看了 spring boot 输出.但是,我看到您没有定义输出目录(在您的示例中已注释掉),因此这只会生成target/generated-sources.我假设您可能曾经尝试过 spring-mvc,然后意外地开始生成 target.

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.

如果您想生成到您的项目结构中,您将总是需要指定output.例如,要写出 ./my-springboot,您需要添加:

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>

这会按预期输出 springboot 代码:

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

如果你想生成一个子模块并且除了排除 supportingFiles 之外你还有一些复杂的逻辑,我提到这是因为你已经在你的例子中注释掉了,你可以定义一个外部忽略文件.

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.

假设您不想修改 pom、属性、README 以及您已创建(或计划)的任何实现文件……在您的项目根目录中创建一个名为 my-springboot.ignore:

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

我还建议将生成器更新到版本 5.0.0-beta2.

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

这是我用来在本地测试的完整插件节点,包括上面的所有建议.

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>

这篇关于如何使用 OpenAPITools 创建 Spring-Boot REST 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆