【问题标题】:OpenApi 3.0.2 | Spring Server Generator | Api/Controller interface namingOpenApi 3.0.2 | Spring 服务器生成器 | Api/Controller 接口命名
【发布时间】:2019-08-26 08:52:59
【问题描述】:

我正在尝试为 OpenApi 3.0.2 规范测试生成的服务器端 Spring MVC 代码。

这是“路径”之一的样子:-

paths:
  /api/v1/int/integrations/{some-path-variable}/some-action:
    get:
      summary: Summary
      description: How to change the generated Api/Controller class name?
      operationId: methodName
      tags:
        - inventory
      parameters:
        - name: Authorization
      other details....

使用 Maven 插件生成服务器端代码,该插件配置为:-

    <plugin>
        <groupId>org.openapitools</groupId>
        <artifactId>openapi-generator-maven-plugin</artifactId>               
        <version>4.1.0</version>           

        <executions>
            <execution>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <inputSpec>${project.basedir}/src/main/resources/open-api/myapi.yaml</inputSpec>
                    <generatorName>spring</generatorName>
                    <library>spring-boot</library>
                    <output>${project.build.directory}/generated-openapi/spring</output>
                    <generateApis>true</generateApis>
                    <addCompileSourceRoot>true</addCompileSourceRoot>
                    <artifactVersion>${project.version}</artifactVersion>
                    <groupId>com.company.division</groupId>
                    <artifactId>myapi-api</artifactId>
                    <generateApiTests>true</generateApiTests>
                    <modelPackage>com.company.division.myapi.generated.model</modelPackage>
                    <apiPackage>com.company.division.myapi.generated.api</apiPackage>
                    <generateApiDocumentation>true</generateApiDocumentation>

                    <configOptions>
                        <dateLibrary>java8</dateLibrary>
                        <java8>true</java8>
                        <interfaceOnly>true</interfaceOnly>
                        <reactive>false</reactive>
                        <useBeanValidation>true</useBeanValidation>
                        <performBeanValidation>true</performBeanValidation>
                        <useOptional>false</useOptional>
                        <serviceInterface>true</serviceInterface>
                        <serviceImplementation>false</serviceImplementation>
                    </configOptions>
                </configuration>

            </execution>
        </executions>
    </plugin>

从插件配置中可以看出,我只对生成模型类和 Spring 控制器接口/API 接口感兴趣。

问题

对于提到的 OpenAPI 规范,生成的 MVC 控制器接口被命名为ApiApi。我猜这是源自路径的开始部分。我可以去掉/api/v1/int 部分,但这会生成名为IntegrationsApi 的接口,但我不想将其命名为InventoryApi。 我们有什么选项来控制生成的控制器界面?

【问题讨论】:

    标签: spring-mvc openapi openapi-generator


    【解决方案1】:

    'useTags' 创建接口和控制器类名

    在插件配置中设置useTags configOption 解决了我的问题:-

                            <configuration>
                                <configOptions>
                                    <useTags>true</useTags>
                                    <dateLibrary>java8</dateLibrary>
                                    <java8>true</java8>
                                    <interfaceOnly>true</interfaceOnly>
                                    <reactive>false</reactive>
                                    <useBeanValidation>true</useBeanValidation>
                                    <performBeanValidation>true</performBeanValidation>
                                    <useOptional>false</useOptional>
                                    <serviceInterface>true</serviceInterface>
                                    <serviceImplementation>false</serviceImplementation>
                                </configOptions>
                            </configuration>
    

    【讨论】:

    • 从命令行你可以添加这个参数附加--additional-properties=useTags=true
    猜你喜欢
    • 2022-07-02
    • 2022-07-23
    • 1970-01-01
    • 2022-12-03
    • 2022-09-30
    • 1970-01-01
    • 2020-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多