如何从不同的模块生成客户端API

时间:2020-08-04 20:51:29

标签: java maven swagger microservices swagger-codegen

我正在构建2个微服务。一个名为购物车,另一个名为产品。他们两个都有通过swagger-codegen-maven-plugin生成的API,但是现在我希望购物车微服务将通过驻留在产品模块中的yaml文件定义生成客户端api(产品从中生成服务器的那个api)客户)。我该如何实现?有没有办法从其他模块访问yaml文件?我必须具有哪种依赖性?

            <plugin>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-codegen-maven-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <id>generate-cart-server</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/resources/static/api.yaml</inputSpec>
                        <language>spring</language>
                        <configOptions>
                            <dateLibrary>joda</dateLibrary>
                        </configOptions>
                    </configuration>
                </execution>
            </executions>
        </plugin>

Project structure

1 个答案:

答案 0 :(得分:1)

只要您在同一个项目中,就可以使用相对路径。您必须添加多个execution标签以同时生成产品的客户端和购物车的服务器。

<executions>
    <execution>
        <id>generate-cart-server</id>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <inputSpec>${project.basedir}/src/main/resources/static/api.yaml</inputSpec>
            <language>spring</language>
            <configOptions>
                <dateLibrary>joda</dateLibrary>
            </configOptions>
        </configuration>
    </execution>
    <execution>
        <id>generate-product-client</id>
        <goals>
            <goal>generate</goal>
        </goals>
        <configuration>
            <inputSpec>${project.basedir}/../product/src/main/resources/static/api.yaml</inputSpec>
            <language>java</language>
            <library>resttemplate</library>
            <configOptions>
                <dateLibrary>joda</dateLibrary>
            </configOptions>
        </configuration>
    </execution>
</executions>

使用<language>java</language>生成Java客户端代码,然后选择要与<library>...</library>一起使用的库。 RestTemplate是Spring的一部分,但还有许多其他内容。

引用Baeldung

 Swagger Codegen supports the following Java libraries (pairs of HTTP 
clients and JSON processing libraries):

    jersey1 – Jersey1 + Jackson
    jersey2 – Jersey2 + Jackson
    feign – OpenFeign + Jackson
    okhttp-gson – OkHttp + Gson
    retrofit (Obsolete) – Retrofit1/OkHttp + Gson
    retrofit2 – Retrofit2/OkHttp + Gson
    resttemplate – Spring RestTemplate + Jackson
    rest-easy – Resteasy + Jackson

当然,您还需要将所选库添加到依赖项中。

相关问题