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
pom.xml

<?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">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.programinjava.learn</groupId>
	<artifactId>SwaggerDemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

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

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath /> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		<!-- added for swagger -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.7.0</version>
		</dependency>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.7.0</version>
		</dependency>
		<!-- swagger added end -->

		<!-- H2 Database -->
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>
The main import dependency in this pom is below one
<!-- added for swagger -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.7.0</version>
		</dependency>

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.7.0</version>
		</dependency>
		<!-- 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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {                                    
	@Bean
	public Docket api() { 
		return new Docket(DocumentationType.SWAGGER_2)  
				.select()                                  
				.apis(RequestHandlerSelectors.any())              
				.paths(PathSelectors.any())                          
				.build();                                           
	}
	
	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")
				.termsOfServiceUrl("https://www.programinjava.com")
				.contact("admin@programinjava.com").license("Free License")
				.licenseUrl("admin@programinjava.com").version("1.0").build();
	}
}

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
http://localhost:8080/swagger-ui.html

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
noeik

0 comments:

Post a Comment