Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate java client code for swagger REST API documentation

Tags:

java

rest

swagger

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:

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.

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 

But this maven project code is a server code. It has annotations like @RequestMapping in PetApi.java and also has a WebMvcConfiguration.class.

I do not want to have a server-stub. I want to have a client-library for the 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?

Thanks for the answers!

like image 202
csikos.balint Avatar asked Oct 26 '15 20:10

csikos.balint


People also ask

How do I generate swagger documentation for REST API automatically?

Use Swagger Inspector to quickly generate your OAS-based documentation for existing REST APIs by calling each end point and using the associated response to generate OAS-compliant documentation, or string together a series of calls to generate a full OAS document for multiple API endpoints.

How do I create a REST Client?

Generate API code with swagger codegen tool To further generate source code, this swagger. yaml file will be the source of input. To facilitate it, Swagger CodeGen tool is used. Swagger provides utility jar to generate client REST clients for different programming languages and framework.

What is swagger code generator?

The Swagger Codegen is an open source code-generator to build server stubs and client SDKs directly from a Swagger defined RESTful API. The source code for the Swagger Codegen can be found in GitHub.


2 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. 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.

For more options on customizing the output of https://generator.swagger.io, please refer to 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)

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

like image 87
William Cheng Avatar answered Sep 29 '22 22:09

William Cheng


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.

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.

Hope it helps you, Thierry

like image 30
Thierry Templier Avatar answered Sep 29 '22 22:09

Thierry Templier