csikos.balint csikos.balint - 1 year ago 310
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;

Expexted output:
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
and also has a

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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download