Generating a Java client from Swagger

The Swagger framework is packaged with the Swagger code generation tool as well (swagger-codegen-cli), which allows you to generate client libraries by parsing the Swagger documentation file. You can download the swagger-codegen-cli.jar file from the Maven central repository by searching for swagger-codegen-cli in http://search.maven.org. Alternatively, you can clone the https://github.com/swagger-api/swagger-codegen Git repository and build the source locally by executing mvn install.

Once you have swagger-codegen-cli.jar locally available, run the following command to generate the Java client for the REST API described in Swagger:

    java -jar swagger-codegen-cli.jar generate 
    -i <Input-URI-or-File-location-for-swagger.json> 
    -l <client-language-to-generate>  
    -o <output-directory>
  

The following example illustrates the use of this tool:

    java -jar swagger-codegen-cli-2.1.0-M2.jar generate 
    -i http://localhost:8080/hrapp/webresources/swagger.json 
    -l java  
    -o generated-sources/java
  

When you run this tool, it scans through the RESTful web API description available at http://localhost:8080/hrapp/webresources/swagger.json and generates a Java client source in the generated-sources/java folder.

Note that the Swagger code generation process uses the mustache templates for generating the client source. If you are not happy with the generated source, Swagger lets you specify your own mustache template files. Use the -t flag to specify your template folder. To learn more, refer to the README.md file at https://github.com/swagger-api/swagger-codegen.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.103.96