生成来自不同模块的客户端 API 的方法

huangapple go评论58阅读模式
英文:

How to generate client api from different module

问题

我正在构建两个微服务。一个名为cart,第二个名为product。它们都通过swagger-codegen-maven-plugin生成了API,但现在我想让cart微服务从位于product模块中的yaml文件定义中生成客户端API(product生成了服务器客户端的那个模块)。我该如何实现这一点?是否有一种方式可以从不同的模块访问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>

生成来自不同模块的客户端 API 的方法


<details>
<summary>英文:</summary>

I&#39;m building 2 microservices. One named **cart**, and second one named **product**. Both of them have API generated via `swagger-codegen-maven-plugin`, however now I want that **cart** microservice will have generated client api from yaml file deffinition which resides in product module (the one from which product have generated server client). How can I achive that? Is there a way to access yaml file from different module? What kind of dependency I have to have?

                &lt;plugin&gt;
                &lt;groupId&gt;io.swagger&lt;/groupId&gt;
                &lt;artifactId&gt;swagger-codegen-maven-plugin&lt;/artifactId&gt;
                &lt;version&gt;2.3.1&lt;/version&gt;
                &lt;executions&gt;
                    &lt;execution&gt;
                        &lt;id&gt;generate-cart-server&lt;/id&gt;
                        &lt;goals&gt;
                            &lt;goal&gt;generate&lt;/goal&gt;
                        &lt;/goals&gt;
                        &lt;configuration&gt;
                            &lt;inputSpec&gt;${project.basedir}/src/main/resources/static/api.yaml&lt;/inputSpec&gt;
                            &lt;language&gt;spring&lt;/language&gt;
                            &lt;configOptions&gt;
                                &lt;dateLibrary&gt;joda&lt;/dateLibrary&gt;
                            &lt;/configOptions&gt;
                        &lt;/configuration&gt;
                    &lt;/execution&gt;
                &lt;/executions&gt;
            &lt;/plugin&gt;
[![Project structure][1]][1]


  [1]: https://i.stack.imgur.com/pd5bR.png

</details>


# 答案1
**得分**: 2

您可以在同一项目内使用相对路径。您必须添加多个`execution`标签以生成产品客户端和购物车服务器的代码。

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

使用&lt;language&gt;java&lt;/language&gt;生成Java客户端代码,并选择要与&lt;library&gt;...&lt;/library&gt;一起使用的库。RestTemplate是Spring的一部分,但还有许多其他选择。

引用自Baeldung

Swagger Codegen支持以下Java库(HTTP客户端和JSON处理库的组合):

    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

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

英文:

You can use relative paths as long as you are inside the same project. You have to add multiple execution tags to generate both, client for product and server for cart.

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

Use &lt;language&gt;java&lt;/language&gt; to generate Java client code and choose a library you'd like to use with &lt;library&gt;...&lt;/library&gt;. RestTemplate is a part of Spring, but there are many others.

Quoting from 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

Of course, you need to add the selected library to your dependencies as well.

huangapple
  • 本文由 发表于 2020年8月5日 04:51:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63254860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定