1. Swagger 2.0 集成配置
.添加Swagger2的Maven依赖
<dependencies>
<!-- Swagger2 是注解 @EnableSwagger2-->
<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>
<!--swagger 用于引入 APIOperation Api注解 用于展示接口文档 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.12</version>
</dependency>
<dependencies>
2. 创建Swagger配置文件
package com.example.springbootproject.Swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.RequestHandler;
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 createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.springbootproject")) // 配置需要扫描的包路径
.paths(PathSelectors.any())
.build();
//.globalOperationParameters(pars);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("身份认证服务")
.description("提供统一的身份认证与身份验证服务")
.termsOfServiceUrl("http://www.baidu.com")
.version("0.0.1")
.build();
}
}
这里是扫描到这个路径下的RequestController的类,如果不在这个类扫描就会出现doc文件中不存在
创建配置文件 这个类要和启动类放在同一个目录下,
用@Configuration注解该类,等价于XML中配置beans;用@Bean标注方法等价于XML中配置bean。Application.class 加上注解@EnableSwagger2 表示开启Swagger,也可以在配置文件上加@EnableSwagger2
只要swagger扫描到@RestController注解的类就会按默认配置生成接口文档
注意:com.example.springbootproject
它是模糊匹配的,所以我们在创建包还有URL时要避免这种格式。