csikos.balint csikos.balint - 3 months ago 58
Java Question

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:

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!

Answer

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