1、配置pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2、粘贴Swagger2代码
首先建立config文件夹,然后新建Swagger2.java文件,粘贴代码
package com.example.szlzzhcu_ht.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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 Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo()) //apiInfo()用来创建该Api的基本信息(这些基本信息会展现在文档页面中)
.select() //返回一个ApiSelectorBuilder实例用来控制哪些接口暴露给Swagger来展现
为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.nercita.szlzzhc"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.version("1.0")
.build();
}
}
http://localhost:端口号/swagger-ui.html#! 启动项目数据网址就能点开