Swagger-codegen as a maven dependency

Swagger is a project which is developing rapidly these days. And around the world a lot of people are using swagger for their API representation. Swagger provides a lot of supporting tools and swagger-codegen is a key tool that people are using these days. In one of my previous posts I’ve talked about swagger-codegen and I’ve told you guys how to use swagger codegen. But I found that there is no documentation telling how to use swagger-codegen as a maven dependency.

What is a maven dependency?

  • Dependency management is one of the features of Maven that is best known to users and is one of the areas where Maven excels. There is not much difficulty in managing dependencies for a single a project, but when you start getting into dealing with multi-module projects and applications that consist of tens or hundreds of modules this is where Maven can help you a great deal in maintaining a high degree of control and stability.
  • Refer this to learn more about maven dependencies.

Swagger codegen as a maven dependency

  • You can find the latest version of swagger-codegen maven dependency from here.
  • Add the dependency into your pom file.
  • Now you need to do following three steps to generate your client side code.
    1. Set generator configurations 
      • Import io.swagger.codegen.config.CodegenConfigurator class and create an object.
      • Using set methods in that class you can set the configurations.
    2. Set client inputs
      • import io.swagger.codegen.ClientOptInput class and create ann object using the configuratore created in the above step.
        example:-
        final ClientOptInput clientOptInput = configurator.toClientOptInput();
    3. Now generate the client SDK
      • Import io.swagger.codegen.DefaultGenerator class and call the following method.
        new DefaultGenerator().opts(clientOptInput).generate();
  • All the work is done now. Feel free comment bellow if you have any doubts. 

Leave a Reply