如何为 swagger REST API 文档生成 java 客户端代码

How to generate java client code for swagger REST API documentation(如何为 swagger REST API 文档生成 java 客户端代码)

本文介绍了如何为 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 客户端代码