Tuesday, May 1, 2018

Implementation of Swagger in spring boot

Swagger is one of the most used documentation tool for the software engineering , when you have the APIs exposed to the outer world and you need to maintain the documents of Input and Output Contract or you need to see the sample input and expected sample output of the api , swagger is worth use.

What is Swagger  ?
Swagger is an open source software framework backed by a large ecosystem of tools that helps developers design, build, document, and consume RESTful Web services.Wikipedia

Implementation of swagger in spring boot :
spring boot provide very easy implementation of swagger tool and you just need to write few line of code along with few jars , your documentation tool is ready to use.

Now let see the coding implementation

Pseudo Code :
First we will create 2 rest apis , one will be get and second will be post
post endpoint will take input as user dto and get will use to fetch the user.
then after we will add the swagger dependency in pom.xml 
Now we will implement swagger code.
after that we are done and will run the server and will show you the swagger ui.

Implementation Code : ( Can download it from GitHub as well)
First we will see the code structure of projects
Now we will see the pom.xml how it looks

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


	<description>Demo project for Spring Boot</description>

		<relativePath /> <!-- lookup parent from repository -->


		<!-- added for swagger -->

		<!-- swagger added end -->

		<!-- H2 Database -->


The main import dependency in this pom is below one
<!-- added for swagger -->

		<!-- swagger added end -->
These will add the Swagger dependency in file.
Now we will add new class in project which is our swagger configuration class .
package name : com.programinjava.learn.config
class name : SwaggerConfig.java

package com.programinjava.learn.config;

import static com.google.common.base.Predicates.or;
import static springfox.documentation.builders.PathSelectors.regex;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.base.Predicate;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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;

public class SwaggerConfig {                                    
	public Docket api() { 
		return new Docket(DocumentationType.SWAGGER_2)  
	private Predicate<String> postPaths() {
		return or(regex("/api/**"));

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("Program in java APIs")
				.description("Program in java APIs reference for developers")
				.contact("admin@programinjava.com").license("Free License")

Thats it , you have just implemented the swagger in your project. No further action required.

How to see the swagger UI  ?
You check the endpoint docs you need to hit the below URL

and your swagger page will look like this :
Hope this will help you in implementing swagger in your project.
Read More:
  1. Spring Boot and Hibernate Tutorials - Application using Spring Boot
  2. What is Spring boot ?
  3. How to read value from property file in spring boot ?
  4. First Rest API Application - Top Spring Framework Interview Questions

If you have any issue while implementing the swagger , leave us a comment , will be happy to help
If you like this post please share

Thanks for reading


Post a Comment