1、pom.xml 配置
<properties>
<java.version>1.8</java.version>
<swagger.version>3.0.0</swagger.version>
<swagger-model.version>1.6.6</swagger-model.version>
<swagger-ui.version>1.9.6</swagger-ui.version>
<spring-plugin-core.version>2.0.0.RELEASE</spring-plugin-core.version>
</properties>
<!-- Swagger 依赖配置 -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${swagger-ui.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.plugin</groupId>
<artifactId>spring-plugin-core</artifactId>
<version>${spring-plugin-core.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-model.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger-model.version}</version>
</dependency>
2、SwaggerConfig.java
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket productApi() {
//添加head参数start
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
tokenPar.name("token").description("令牌").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.select()
// 扫描的包路径
.apis(RequestHandlerSelectors.basePackage("com.xxx.XXX.controller"))
// 定义要生成文档的Api的url路径规则
.paths(PathSelectors.any())
.build()
.globalOperationParameters(pars)
// 设置swagger-ui.html页面上的一些元素信息。
.apiInfo(metaData());
}
// 自定义swagger数据源
private ApiInfo metaData() {
return new ApiInfoBuilder()
// 标题
.title("XXX集成Swagger2")
// 描述
.description("XXX项目接口文档")
// 文档版本
.version("1.0.0")
.license("Apache License Version 2.0")
.licenseUrl("https://www.apache.org/licenses/LICENSE-2.0")
.build();
}
}
3、启动项目。访问swagger文档,http://localhost:8080/swagger-ui/index.html#/ (注意:记得修改接口)