Friday, 5 May 2017

Generate Java Client Library For NodeJS using Swagger Codegen


This blog will give you a overview on generating Java client library for NodeJS project using Swagger Codegen.

Step-by-step guide

Add the steps involved:
  1. Install Swagger Codegen in your machine as detailed in page
  2. Make sure that we have endpoint in NodeJS project that exposes Swagger JSON Definition (/api-docs is exposed with Swagger Definition)
  3. Validate the Swagger Definition with help of Swagger Editor 
  4. Invoke Code using the following command:
    • swagger-codegen generate -i http://localhost:18138/api-docs -l java -o flex/executionconfig -c config.json --api-package com.ooyala.flex --artifact-id executionConfiguration --artifact-version 0.0.1
  5.  Swagger Codegen options that is been used are described as follows:
    • -i --> Spec file (JSON in our case)
    • -l --> language for which the client library has to generated
    • -o --> output directory
    • -c --> custom configuration file location
    • --api-package --> respective package name
    • --artifact-id --> respective artifact ID
    • --artifact-version --> version for the client      
  6.  The following custom config.json file that is used
    {
      "library":"feign",
    }
  7. codegen will create a src for client library similar to the following:
  8. Generated code will have README.md which will help us with the next steps
  9. To generate java client library and deploy to repository use any of the following based on the need
    • mvn install
    • man deploy
  10. Once the package/library is created we can use it in our java project by injecting the dependency

No comments:

Post a Comment