Swagger Code Gen Retrofit2 library

Generating code using swagger-codegen is an easy way to get your client up and running using different languages and libraries. In this tutorial I will show how to create clients in java using retrofit2 library.

Basics

First you need to download swagger-codegen library to your computer using next command. You might want to recheck the current version in the github swagger-codegen repository here.

Next you want to execute command for generating the code. Here there are several mandatory parameters you need to add:

  • url to the api-docs
  • target language
  • library to be used
  • api and model package
  • output folder

java -jar swagger.jar generate -i http://localhost:8080/v2/api-docs ¬†-l java –library=retrofit2 -DmodelPackage=rs.pscode.start.model,apiPackage=rs.pscode.start.api -o spring-boot-starter-genereated

Retrofit2

Generating retrofit 2 client code can be done using next command

Here we have all necessary parameters like URL to the api-docs, language, library, packages and output folder.