Spring Boot启用Swagger2
Swagger2是一个开源项目,用于为RESTful Web服务生成REST API文档。 它提供了一个用户界面,可通过Web浏览器访问RESTful Web服务。
要在Spring Boot应用程序中启用Swagger2,需要在构建配置文件中添加以下依赖项。
<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>
对于Gradle用户,请在build.gradle 文件中添加以下依赖项。
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.7.0'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.7.0'
现在,在主Spring Boot应用程序中添加@EnableSwagger2
注释。 @EnableSwagger2
注释用于为Spring Boot应用程序启用Swagger2。
主Spring Boot应用程序的代码如下所示 -
package com.yiibai.swaggerdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@SpringBootApplication
@EnableSwagger2
public class SwaggerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerDemoApplication.class, args);
}
}
接下来,创建Docket Bean以为Spring Boot应用程序配置Swagger2。需要定义一个基础包来为Swagger2配置REST API。
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.yiibai.swaggerdemo")).build();
}
现在,在主Spring Boot应用程序类文件中添加这个bean,主Spring Boot应用程序类看起来如下所示 -
package com.yiibai.swaggerdemo;
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 SwaggerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerDemoApplication.class, args);
}
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("com.yiibai.swaggerdemo")).build();
}
}
现在,在构建配置文件中添加以下Spring Boot Starter Web依赖项以编写REST端点,如下所示 -
Maven用户可以在pom.xml 文件中添加以下依赖项 -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Gradle用户可以在build.gradle 文件中添加以下依赖项 -
compile('org.springframework.boot:spring-boot-starter-web')
现在,这里显示了在Rest Controller文件中构建两个简单的RESTful Web服务GET和POST的代码 -
package com.yiibai.swaggerdemo;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class SwaggerAPIController {
@RequestMapping(value = "/products", method = RequestMethod.GET)
public List<String> getProducts() {
List<String> productsList = new ArrayList<>();
productsList.add("Honey");
productsList.add("Almond");
return productsList;
}
@RequestMapping(value = "/products", method = RequestMethod.POST)
public String createProduct() {
return "Product is saved successfully";
}
}
完整的构建配置文件如下 -
Maven构建文件 - 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.yiibai</groupId>
<artifactId>swagger-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>swagger-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.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-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