32

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!

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
csikos.balint
  • 1,107
  • 2
  • 10
  • 25

6 Answers6

21

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

UPDATE: On May 2018, about 50 top contributors and template creators of Swagger Codegen decided to fork Swagger Codegen to maintain a community-driven version called OpenAPI Generator. Please refer to the Q&A for more information.

William Cheng
  • 10,137
  • 5
  • 54
  • 79
12

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

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • 1
    Thanks for the answer. I cannot uderstand how could I miss this section in the manual but this was exectly the answer I needed. – csikos.balint Oct 27 '15 at 09:37
12

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:

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. With generator.swagger.io you can also decide which library to use. There might be an enhancement to editor.swagger.io to be able to select the library to use as well. To consider is that the swagger.io options are completely free, whereas Restlet and APIMATIC are freemium.

st.huber
  • 1,481
  • 2
  • 24
  • 45
  • 1
    editor.swagger.io uses generator.swagger.io, which is also powerd by the swagger codegen project, to generate API clients, server stubs, and API documentation. – William Cheng Sep 01 '16 at 07:29
  • @wing328 You're right and I know that it's still makes a difference if you use the swagger-codegen project or the online version (generator.swagger.io). Online you cannot select with which library should be used for the conversion. – st.huber Sep 01 '16 at 09:05
  • You can. Please refer to https://github.com/swagger-api/swagger-codegen#online-generators on how to pass various options to customize the output. For editor.swagger.io, there's a discussion to add a menu to customize the output as well: https://github.com/swagger-api/swagger-editor/issues/713 – William Cheng Sep 02 '16 at 07:03
  • Thanks for your input I updated my answer accordingly. Please feel free to up vote if it's alright for you now – st.huber Sep 02 '16 at 07:58
  • just for clarification, the final URL for the pet store is https://petstore.swagger.io:443/v2/swagger.json , otherwise swagger-codegen-cli.jar will throw an com.fasterxml.jackson.core.JsonParseException – devwebcl Dec 21 '20 at 15:47
4

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

zygimantus
  • 3,649
  • 4
  • 39
  • 54
2

Just a silly extension to @wing328's answer.

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)

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

Instead of using swaggerUrl with an URL to the OpenAPI/Swagger spec, you can also include the spec in the JSON payload with spec, e.g.

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

More Info: Official Doc

so-random-dude
  • 15,277
  • 10
  • 68
  • 113
  • I don't think this is related to the topic. I think usage or even installing 'curl' command on UNIX systems is a different question. – csikos.balint May 14 '17 at 14:42
  • @csikos.balint It is related because when I tried, it resulted in certificate error... I was not adding random options of curl here. – so-random-dude May 14 '17 at 21:18
-6

Although swagger generator generates a Java SDK, APIMATIC sdk's are quite mature, detailed, and gives more flexibility that Swagger Gen. You should try APIMATIC sdk generator, you're gonna love it.

Waseem Hassan
  • 233
  • 2
  • 10