Document Spring based Restful API using Swagger

| By Webner

Swagger is a very powerful tool to generate documentation of your Rest based web services API containing Spring annotations. You don’t need to add any type of comments to your code to generate documentation. It will pick the Spring annotations and will generate the documentation for you. Below are the steps that you have to follow to document your api with Swagger :

1.  Add below specified maven dependencies to your project :


Version specified inside maven properties element in the POM  file :


2.  Add you can use any class name) class to your project and add below code to it :

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
public class SwaggerConfiguration {
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).select().apis(

3.  Match you application.xml file with content below and if anything is missing add it to your file :

<beans xmlns=""
xmlns:mvc="" xmlns:xsi=""
xsi:schemaLocation="    ">
<context:component-scan base-package="add your base package here" />
<context:annotation-config />
<mvc:annotation-driven />
<mvc:resources mapping="swagger-ui.html"   location="classpath:/META-INF/resources/" />
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />

4.  After completing above changes, compile and deploy your project.  

Type url : https://your api url /swagger-ui.html  , You will see the swagger api interface as below :


All the above controller classes will be represented here and hyperlinked to the methods defined in the respective controller class.

For example, customer Services.( for me), on clicking it  you will see all its methods :


Leave a Reply

Your email address will not be published. Required fields are marked *