How to generate java client code for swagger REST API documentation(如何为 swagger REST API 文档生成 java 客户端代码)
问题描述
我的场景如下.
我有一个招摇的 .json 例如:http://petstore.swagger.io/v2/swagger.json我想为上面的 REST API 使用生成的 java 客户端,例如:
I have a swagger .json eg.: http://petstore.swagger.io/v2/swagger.json I want to use a generated java client for the REST API above, like:
PetApi petApi = new PetApi();
Pet pet = new Pet;
pet.setName("cica");
pet.setId(1L);
petApi.addPet(pet);
System.out.println(petApi.getById(1L));`
扩展输出:cica
,新宠物按照REST API实现存储.
Expexted output: cica
and the new pet is stored according to the REST API implmentation.
我已使用以下命令成功为 petstore 生成了服务器存根:
I have successfully generated server stub for the petstore with the command:
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate
-i http://petstore.swagger.io/v2/swagger.json
-l spring-mvc
-o samples/server/petstore/spring-mvc
但是这个 maven 项目代码是一个服务器代码.它在 PetApi.java
中有类似 @RequestMapping
的注解,并且还有一个 WebMvcConfiguration.class
.
But this maven project code is a server code. It has annotations like @RequestMapping
in PetApi.java
and also has a WebMvcConfiguration.class
.
我不想拥有服务器存根.我想要一个用于 petstore REST API 的客户端库.
I do not want to have a server-stub. I want to have a client-library for the petstore REST API.
有没有工具可以为我生成合适的客户端库?我应该修改服务器存根,因此它具有所有模型还是应该使用简单的 springRestTemplate?
Is there a tool which can generate the appropriate client library for me? Should I modify the server-stub, hence it has all the models or should I use a simple springRestTemplate?
感谢您的回答!
推荐答案
我觉得你对Swagger Codegen的参数-l
的取值不对(你用的是spring-mvc
是一种服务器端技术).您可以尝试使用值 java
.
I think that you don't use the right value for the parameter -l
of Swagger Codegen (you use spring-mvc
which is a server-side technology). You could try to use the value java
.
您还可能注意到有一个工具,Restlet Studio,它允许从 Swagger 生成代码内容.对于 Java,它主要依赖于 Restlet 框架,但我认为它可以满足您的需求.
You could also notice that there is a tool, the Restlet Studio, that allows to generate code from Swagger content. For Java, it mainly relies on the Restlet framework but I think that it could suit your needs.
希望对你有帮助蒂埃里
这篇关于如何为 swagger REST API 文档生成 java 客户端代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何为 swagger REST API 文档生成 java 客户端代码


基础教程推荐
- 大摇大摆的枚举 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 从 python 访问 JVM 2022-01-01