Spring Boot — Swagger2 | Code Factory
Reference Link : Link
Donate : Link
Swagger2 is an open source project used to generate the REST API documents for RESTful web services. It provides a user interface to access our RESTful web services via the web browser.
Required dependency to enable Swagger in your Spring Boot Application.
<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>
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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.swagger2</groupId>
<artifactId>SpringBootSwagger2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootSwagger2</name>
<description>Demo project for Spring Boot Swagger2</description><properties>
<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-test</artifactId>
<scope>test</scope>
</dependency><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>
</dependencies><build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build></project>
Add the @EnableSwagger2
annotation in your main Spring Boot application. The @EnableSwagger2
annotation is used to enable the Swagger2 for your Spring Boot application.
Next, create Docket Bean to configure Swagger2 for your Spring Boot application.
SpringBootSwagger2Application.java
package com.swagger2;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@SpringBootApplication
@EnableSwagger2
public class SpringBootSwagger2Application {public static void main(String[] args) {
SpringApplication.run(SpringBootSwagger2Application.class, args);
}@Bean
public Docket TestApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage("com.swagger2")).build();
}
}
Swagger2Controller.java
package com.swagger2.controller;import java.util.ArrayList;
import java.util.List;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class Swagger2Controller {@GetMapping(value = "/items")
public List<String> getItems() {
List<String> items = new ArrayList<String>();
items.add("Item1");
items.add("Item2");
return items;
}
@PostMapping(value = "/items")
public String saveItem() {
return "Item Saved";
}
}
Start your Spring Boot Application and hit URL http://localhost:8080/swagger-ui.html