英文:
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>
<details>
<summary>英文:</summary>
I'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?
<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]][1]
[1]: https://i.stack.imgur.com/pd5bR.png
</details>
# 答案1
**得分**: 2
您可以在同一项目内使用相对路径。您必须添加多个`execution`标签以生成产品客户端和购物车服务器的代码。
```xml
<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支持以下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.
<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>
Use <language>java</language>
to generate Java client code and choose a library you'd like to use with <library>...</library>
. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论