Swagger - How to implement in Java?

Why Swagger?

Java REST (Representational State Transfer) is a most common way to expose web services, but how can any developer or third party will understand or utilize those services easily? There is no such straightforward rules or standards to write those services.

To use web services by third person, we have to manually create document and it will be very difficult to keep perfectly synchronized that document with the API.

Key points to maintain API documentation.
- URL's.
- Input format - JSON
- Input data types - String, Integer, Date .... etc
- Output.
- Error codes and messages.



To overcome all these challenges, there is another way to create documentation in the code it self by using Swagger framework.

Swagger

Swagger is a simple yet powerful representation of your RESTful API, Swagger defines standard language interface to REST APIs which allow both humans and computers to discover and understand the capabilities of the service without access to source code, documentation.

Developer can write their web services using any popular framework, and top of the web services they needs to define some simple annotation provided by swagger, when application will start swagger scans your code and exposes documentation based on the defined annotation. Without an extra efforts any developer, tester or client can consume this URL and learn how to use web services or can test this web services.

Implementation of Swagger in Java REST services

Consider below simple Rest service which accepts input as a String and return back response in String.

 @GET  
 @Path("/printMessage")  
 @Produces(MediaType.APPLICATION_JSON)  
 public String printMessage(String msg) {  
           return "Hello : " + msg;  
 }       

To export this service to third party, you need to provide documentation saying the URL, Request type, Input parameters, Response type, Data types .... etc

Now by using swagger we can easily show all above information, please look below image stating all information about above Rest service.

Swagger URL for this application - http://localhost:8080/RestEasy

By looking at above image you have all the information about above Rest service, no need to create manually documentation.
  • Name of the Rest service with description.
  • URL with description.
  • Type of Rest service - GET.
  • Return response - String class
  • Number of parameters with description and data type.
  • All the list of operations.
Now to implement Swagger we need to follow below simple steps.

Code is already checked-in to Git repository - Developer-Tech

  • Add pom dependency.
 <dependency>  
           <groupId>com.wordnik</groupId>  
           <artifactId>swagger-jaxrs_2.9.1</artifactId>  
           <version>1.3.0</version>  
 </dependency>  


  • Show Swagger ui page -  Please copy css, images, lib, index.html, swagger-ui.js, swagger-ui.min.js files in to webapp folder.

  • SwaggerRestServiceApplication.java - This is main file is used to create singletons object of Rest service class.
  • web.xml - Depends on your service call please make necessary changes in your web.xml.
 <servlet>  
       <servlet-name>DefaultJaxrsConfig</servlet-name>  
       <servlet-class>com.wordnik.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>  
       <init-param>  
             <param-name>api.version</param-name>  
             <param-value>1.0.0</param-value>  
       </init-param>  
       <init-param>  
             <param-name>swagger.api.basepath</param-name>  
             <param-value>http://localhost:8080/RestEasy</param-value>  
       </init-param>  
       <load-on-startup>2</load-on-startup>  
 </servlet>  

 <servlet-mapping>  
       <servlet-name>resteasy-servlet</servlet-name>  
       <url-pattern>/swagger/*</url-pattern>  
       <url-pattern>/api-docs/*</url-pattern>  
 </servlet-mapping>  

Finally please add Swagger annotation

  • SwaggerRestService.java - For more information regarding annotation please refer Swagger Annotations.
By using appropriate annotations we can also add json format input sample, error codes... etc in Swagger.

Code is available in Git repository - Developer-Tech

For more information, please feel free to contact me.

Build Cordova Application


How to use continuous integration for Cordova application using Microsoft Team Foundation Server


Continuous Integration is software development practice where many developers collaborate on complex software projects, every day usually each developer do many code changes in different project files and it can be a long and unpredictable process to integrate all parts of code together.

Continuous integration (CI) is the process of integrating your code into a shared repository as frequently as possible. During code integration, a build break or a test failure can inform you, in a timely manner, of an error in your code.

Martin Fowler has the following breakdown of practices for continuous integration:

  • Maintain a single source repository.
  • Automate the build.
  • Make your build self-sustaining.
  • Check in at least once a day.
  • Build each check-in on the CI server.
  • Keep the build fast.
  • Test in a clone of the production environment.
  • Make it easy for anyone to get the most recent executable.
  • Always be aware of what is happening.
  • Automate deployment.
This tutorial will cover how to build continuous integration for Cordova application using Microsoft Team Foundation Server.

Build Cordova application workflow 
  • Project setup for CI.
  • Get a latest code from git repository.
  •  Build Android apk file using Cordova.
  • Sign Android apk file using private keystore.
  • Publish Android apk file.

Project setup for CI

Installing extension 

To build Cordova application on Team Foundation Server, we need to add extension from visual studio Marketplace - Cordova Extension

Create build definition

  1. Open your team project in web browser. https://{your_account}.visualstudio.com/DefaultCollection/{your_team_project}
      2. Create build definition.

       3. Select Empty build definition.

        4. Select appropriate Repository and branch.

Build Android apk file using Cordova

1. On empty build definition, 
    click on "Add build step..." >> Add "Cordova Command".
   (Note: If you have installed above mentioned plugin, then only you will able to see this option).

  2. To build Android apk using Cordova CLI , we need to add parameters as shown below.

  • Pull latest code from Git repository.
  • Run Cordova build command for android platform
    • Cordova build --release android.
  • Don't forgot to mention Working Directory path (project git repository path).
After running above build definition, Cordova will build release version of android apk file in your project directory -
{Project Name}\platforms\android\build\outputs\apk\android-release-unsigned.apk

Now next step is we need to copy this generated apk file in to another folder. (This is optional step)

3. From build step add "Command Line".



Above configuration will run simple copy command.
cmd copy source-file-name destination-file-name

By using this step we can rename generated apk file (android-release-unsigned.apk) to meaningful name.

Sign Android apk file using private keystore

1. From build step add "Android Signing".


  • APK Files: Path of generated Android apk file.
  • Jarsigner Options:
    • Keystore File: Keystore file path from git repository, assuming that keystore file is already been added in git repository.
    • Keystore Password: Password of keystore. Password should be encrypted, we can use secrete variables from TFS - BuildDefinationVariables
    • Alias: Name of the alias.
Now save and run above build definition. Sign apk file will get build in the same location.

But still you can't access this signed apk file, because all this build definition are running over the server pool.

To publish/ use this apk file we need to do checked-in to back to git repository.


Publish Android apk file

To publish/use generated android apk file, we need to do check-in to git repository, hence now we are going to use same Command Line tool to run the required git commands.


Now enable continuous integration for all above build task.

On every check-in, this build definition will get trigger and new release version of sign apk file will get build automatically.