Spring Boot REST API Documentation Using Swagger 2 With Springfox

Spring Boot REST API Documentation Using Swagger 2 With Springfox

In this tutorial we are going to learn how to document our Spring Boot REST APIs using Swagger with Springfox.

Tools You Will Need
Maven 3.3+
Your favorite IDE. I'm using NetBeans
JDK 1.8+

Creating the Project With Spring Initializer
Go to start.spring.io and follow the steps below to generate a new project.

Enter the Details as Follows

Group: com.kodnito
Artifact: spring-boot-swagger-springfox
Dependencies: Web

Click Generate Project to generate and download your project.
Next, Unzip the downloaded zip file and import it into your favorite IDE.
Open the pom.xml and add two more dependencies to the file.



Swagger Config
Create MySwaggerConfig class inside com.kodnito.springbootswaggerspringfox.config package and add the following to it.


package com.kodnito.springbootswaggerspringfox.config;

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 MySwaggerConfig {
    public Docket customDocket() {
        return new Docket(DocumentationType.SWAGGER_2)

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Hello Swagger APIs")
                .description("My APIs is listed here")

@EnableSwagger2 annotation enables Springfox Swagger 2
Docket initialize Springfox main bean for configuring Swagger 2 specification
select() returns an instance of ApiSelectBuilder to give control over the endpoints exposed via swagger.
apis() define which classes to be included, you can limit them by a base package, class or method, here we include them all.
paths() you can define which controller methods should be included, we include them all here.
apiInfo() is used to give your api title and description

Spring Boot Rest Controller
Inside com.kodnito.springbootswaggerspringfox.controller package create a simple REST Controller class and add the following to it


package com.kodnito.springbootswaggerspringfox.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Api("This is The Hello Swagger API Documentation")
public class HelloSwaggerController {
    @ApiOperation("Return String hello swagger")
    public String helloSwagger() {
        return "hello swagger";
    @ApiOperation("This is the POST request")
    public String postReq() {
        return "post";

Now, enter the http://localhost:8080/swagger-ui.html in browser's address bar and access your new Swagger documentation.

Share this: