json - 为同一项目中的多个 swagger 生成代码

标签 json swagger swagger-codegen

如何在一个 pom.xml 中的同一模块/项目中为多个 swagger 文件生成代码。

在我的应用程序中,客户端提供了 swagger,我们有一个要调用的后端 API,他们提供了 swagger。我想在同一个项目中为这两个生成代码。我想到的一种方法是创建单独的模块并单独执行插件,并在主模块中调用这些依赖项。

如何从一个构建插件生成代码?如果是转发,请指出现有的一个。我没找到。

这是我在 pom.xml 中配置的插件

 <plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.2.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                      <inputSpec>${project.basedir}/src/main/ resources/Service.json</inputSpec><inputSpec>${project.basedir}/src /main/resources/Client.json</inputSpec>
                        <language>java</language>
                        <configOptions>
                            <dateLibrary>java8</dateLibrary>
                        </configOptions>
                        <modelPackage>com.service.model</modelPackage>
                        <environmentVariables>
                            <models/>
                            <supportingFiles>false</supportingFiles>
                        </environmentVariables>
                    </configuration>
                </execution>
            </executions>
        </plugin>

还尝试过*.json。在任何时候,它都只需要一个 json 文件并生成代码。

最佳答案

为了做到这一点,您可以为每个 json 文件声明不同的执行,每个文件都应该有一个唯一的 id。

这是一个包含两次执行的示例,first.jsonexecution-first-jsonfirst.json 文件的 execution-second-json文件 second.json

<plugin>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-codegen-maven-plugin</artifactId>
    <version>2.2.1</version>
    <executions>
        <execution>
            <id>execution-first-json</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/first.json</inputSpec>
                <language>java</language>
                <configOptions>
                    <dateLibrary>java8</dateLibrary>
                </configOptions>
                <modelPackage>com.service.model</modelPackage>
                <environmentVariables>
                    <models/>
                    <supportingFiles>false</supportingFiles>
                </environmentVariables>
            </configuration>
        </execution>
        <execution>
            <id>execution-second-json</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
              <inputSpec>${project.basedir}/src/main/resources/second.json</inputSpec>
                <language>java</language>
                <configOptions>
                    <dateLibrary>java8</dateLibrary>
                </configOptions>
                <modelPackage>com.service.model</modelPackage>
                <environmentVariables>
                    <models/>
                    <supportingFiles>false</supportingFiles>
                </environmentVariables>
            </configuration>
        </execution>
    </executions>
</plugin>

关于json - 为同一项目中的多个 swagger 生成代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41702500/

相关文章:

javascript - GoJS自定义节点保存默认值

java - 将 JSONArray 拆分为更小的 JSONArray

java - 新端点在测试中导致 IllegalStateException : Invalid mime type "headers": does not contain '/'

java - 将swagger-codgen-maven-plugin用于REST客户端时跳过gradle文件生成

java - Betfair 的 Java Streaming API 入门

json - 在 MQL4 脚本中管理 JSON 数组格式

javascript - 通过调用带参数的 URL 获取 json 对象

kotlin - Swagger-UI和Ktor如何导入swagger.json或.yaml文件启动Swagger-UI?

swagger - OpenApi:如何描述错误代码和消息?

java - SwaggerGen 枚举生成空数据类型