简体   繁体   English

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

[英]How to generate java client code for swagger REST API documentation

My scenario is the following. 我的情况如下。

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: 我有一个招摇.json例如: http//petstore.swagger.io/v2/swagger.json我想为上面的REST API使用生成的java客户端,例如:

PetApi petApi = new PetApi();
Pet pet = new Pet;
pet.setName("cica");
pet.setId(1L);
petApi.addPet(pet);
System.out.println(petApi.getById(1L));`

Expexted output: cica and the new pet is stored according to the REST API implmentation. Expexted输出: cica和新宠物根据REST API实现存储。

I have successfully generated server stub for the petstore with the command: 我已使用以下命令为petstore成功生成服务器存根:

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

But this maven project code is a server code. 但是这个maven项目代码是一个服务器代码。 It has annotations like @RequestMapping in PetApi.java and also has a WebMvcConfiguration.class . 它有注释,如PetApi.java @RequestMapping ,还有一个WebMvcConfiguration.class

I do not want to have a server-stub. 我不想拥有服务器存根。 I want to have a client-library for the petstore REST API. 我想拥有一个petstore REST API的客户端库。

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? 我应该修改服务器存根,因此它有所有模型或者我应该使用简单的springRestTemplate吗?

Thanks for the answers! 谢谢你的回答!

Instead of using the JAR, you can also use https://generator.swagger.io to generate the SDKs (Java, Ruby, PHP, etc) online without installing anything. 您也可以使用https://generator.swagger.io在线生成SDK(Java,Ruby,PHP等)而无需安装任何内容,而不是使用JAR。 Here is an example: 这是一个例子:

curl -X POST -H "content-type:application/json" -d '{"swaggerUrl":"http://petstore.swagger.io/v2/swagger.json"}' https://generator.swagger.io/api/gen/clients/java

and here is a sample response: 这是一个示例响应:

{"code":"1445940806041","link":"https://generator.swagger.io/api/gen/download/1445940806041"}  

You can then download the zipped SDK from the link. 然后,您可以从链接下载压缩的SDK。

For more options on customizing the output of https://generator.swagger.io , please refer to https://github.com/swagger-api/swagger-codegen#online-generators 有关自定义https://generator.swagger.io输出的更多选项,请参阅https://github.com/swagger-api/swagger-codegen#online-generators

(Swagger Generator is part of the Swagger Codegen project (free, open source) that you can run your local Swagger generator as well) (Swagger Generator是Swagger Codegen项目(免费,开源)的一部分,您可以运行本地Swagger生成器)

As of July 2017, the Java API client generator supports the following HTTP libraries: Jersey 1.x & 2.x, Retrofit 1.x & 2.x, okhttp, Feign, RESTEasy, RestTemplate 截至2017年7月,Java API客户端生成器支持以下HTTP库:Jersey 1.x&2.x,Retrofit 1.x&2.x,okhttp,Feign,RESTEasy,RestTemplate

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). 我认为你没有为Swagger Codegen的参数-l使用正确的值(你使用spring-mvc这是一种服务器端技术)。 You could try to use the value java . 您可以尝试使用值java

You could also notice that there is a tool, the Restlet Studio , that allows to generate code from Swagger content. 您还可以注意到有一个工具,即Restlet Studio ,它允许从Swagger内容生成代码。 For Java, it mainly relies on the Restlet framework but I think that it could suit your needs. 对于Java,它主要依赖于Restlet框架,但我认为它可以满足您的需求。

Hope it helps you, Thierry 希望它对你有帮助,蒂埃里

For your scenario your command should look like this 对于您的场景,您的命令应该如下所示

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate
 -i http://petstore.swagger.io/v2/swagger.json
 -l java
 -o samples/server/petstore/spring-mvc

Other options to convert swagger to jave are: 将swagger转换为jave的其他选项包括:

Though with the GitHub project it's up to you to decide which library (jersey, jersey2, okhttp-gson, etc.) to use when converting swagger to Java client or server code. 虽然使用GitHub项目,您可以决定在将swagger转换为Java客户端或服务器代码时使用哪个库(jersey,jersey2,okhttp-gson等)。 With generator.swagger.io you can also decide which library to use . 使用generator.swagger.io,您还可以决定使用哪个库 There might be an enhancement to editor.swagger.io to be able to select the library to use as well. editor.swagger.io可能有一个增强功能,可以选择要使用的库。 To consider is that the swagger.io options are completely free, whereas Restlet and APIMATIC are freemium. 要考虑的是swagger.io选项是完全免费的,而Restlet和APIMATIC是免费增值。

Probably the fastest and easiest way to do it: 可能是最快速,最简单的方法:

  1. wget https://oss.sonatype.org/content/repositories/releases/io/swagger/swagger-codegen-cli/2.2.1/swagger-codegen-cli-2.2.1.jar
  2. java -jar swagger-codegen-cli-2.2.1.jar generate -l <language> -i <pathOrUrlOfSwaggerSpec>

More info here 更多信息在这里

Just a silly extension to @wing328's answer . 只是对@ wing328的答案的愚蠢延伸。

curl -X POST -H "content-type:application/json" -d '{"swaggerUrl":"http://petstore.swagger.io/v2/swagger.json"}' https://generator.swagger.io/api/gen/clients/java

If it results in this error (SSL certificate problem) 如果导致此错误(SSL证书问题)

curl: (60) SSL certificate problem: unable to get local issuer certificate
More details here: https://curl.haxx.se/docs/sslcerts.html

Add a -k switch to curl. 添加-k开关到curl。 Example: 例:

curl -k -X POST -H "content-type:application/json" -d '{"swaggerUrl":"http://petstore.swagger.io/v2/swagger.json"}' https://generator.swagger.io/api/gen/clients/java

Response 响应

{"code":"7e542952-5385-4e34-8cf6-6196722fb18b","link":"https://generator.swagger.io/api/gen/download/7e542952-5385-4e34-8cf6-6196722fb18b"}

Sending the complete swagger spec JSON payload instead of URL 发送完整的swagger规范JSON有效负载而不是URL

Instead of using swaggerUrl with an URL to the OpenAPI/Swagger spec, you can also include the spec in the JSON payload with spec, eg 您可以使用规范在JSON有效负载中包含规范,而不是使用swaggerUrl和OpenAPI / Swagger规范的URL。

 { "options": {}, "spec": { "swagger": "2.0", "info": { "version": "1.0.0", "title": "Test API" }, ... } } 

More Info: Official Doc 更多信息: 官方文件

虽然swagger生成器生成Java SDK,但APIMATIC sdk非常成熟,详细,并且提供了更多Swagger Gen的灵活性。您应该尝试使用API​​MATIC sdk生成器,您会喜欢它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM