Skip to main content

Swagger Documentation with Spring boot application for REST Services

Steps to integrate Swagger Documentation with Spring Boot Application:

Step-1: Dependency addition in Maven

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.6.1</version>
</dependency>


Step-2: Enable Swagger in your application

Use the following annotations(highlighted in bold) in your Spring Boot Application java file:
@SpringBootApplication
@EnableSwagger2
@ComponentScan(basePackages={"com.test.controller"})
public class SpringBootApp extends SpringBootServletInitializer { 

-------------
-------------
}

@ComponentScan will be used to inform swagger which package to scan to generate documentation.

My Complete Boot Application file looks as follows:

import static springfox.documentation.builders.PathSelectors.regex;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@ComponentScan(basePackages={"com.test.controller"})
public class SmartGateApplication extends SpringBootServletInitializer {

final Logger log = Logger.getLogger(getClass().getName());

public static void main(String[] args) throws Exception {
SpringApplication.run(SmartGateApplication.class, args);
}

public void run(String... args) throws Exception {
log.info("Application Started Successfully !!!");
}

@Bean
        public Docket newsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.test.controller")) //Controller's base path
                .paths(regex("/test.*")) // This URL is the parent path of service endpoint
                .build();
    }
    
        @SuppressWarnings("deprecation")
private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger documentation my REST Application")
                .description("Documentation for REST services")
                .termsOfServiceUrl("www.test.ae")
                .contact("dev@test.come")
                .license("DevTeam")
                .licenseUrl("www.test.ae")
                .version("1.0")
                .build();
    }


}

Step-3
That's it. Now our setup is ready and we can start the server and hit the following URL:
http://localhost:8080/swagger-ui.html

You can also generate the documentation in JSON format using the following URL
http://localhost:8080/v2/api-docs


If you face any trouble, please mail me.



Comments

Popular posts from this blog

Know your Repository Statistics

Being in software development, everyone of us must be using some or the other repository to save our work( popularly know as check-in check-out :) ). Recently while working on one of my project I thought of finding out the statistics of our project repository for some management reports. While there are so many tools available in the market to explore the stats, I chose to go with Tortoise SVN tool with some plugins. Following are other tools that can be very useful based on scenarios: -Commit Monitor -Winmerge -Visual SVN -SVN Monitor -CM Synergy from Telelogic -Many more are there If you are using Tortoise SVN and want to know the details(for example : no of java classes checked-in, lines of codes written, developers name, total code base details and many more ) about your repository You can use the following steps to find the details: 1-check if the SVN has been installed and working properly or not by using following command: C:\>svn help It will output something ...

Testing your Webservice Applications using SOAP UI

SOAP UI is a standard desktop application for testing the Web Services projects. It provides full support for debugging, developing and testing of your web services applications. The tool support data driven development and also provides platform for creating test suites where you can create services for regression testing. For example if you want to test the complete flow of your SOA application, you can create Test Suites using SOAP UI and can perform end to end testing of your applications. The test suits can be configured to run in multiple environments (dev, sit, uat or production). Okay, let’s start the working on SOAPUI. I will show you the simple webservice testing that I developed in my last blog. Prerequisites: -You have developed your webservices -Webservice is ready and running on your local server -Installed SOAP UI tool Step-1 Download the SOAPUI tool Step-2 Open the soap UI Tool Setp-3 Right click on the project and choose New SOAP Pro...

Web Services for Java Developers

Hello All and Welcome back. Today, we will take some time to discuss on Web services implementations. When ever we hit the Google button to search the Web Services implementation, we find many flavours of web services available on internet. Sometimes it becomes difficult to decide which implementation should we follow. There are bunch of services already present in the market which have gained so much popularity in terms of simplicity and their clear implementation.  To deploy a successful Java based web service project, it needs end to end knowledge of the technology and the artifices that are used behind the scene to make your software run without any heck. When I started working on SOAP based service I had very minimal knowledge on the supporting specification and standards that are required for consume/expose the service. I have worked on both SOAP based Service as well as Restful service.  In my up-coming blogs, I will be posting my understanding on implementati...