1.版本信息
<swagger-version>2.9.2</swagger-version>
<swagger-bootstrap-ui-version>1.9.6</swagger-bootstrap-ui-version>
2.pom依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger-version}</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>${swagger-bootstrap-ui-version}</version>
</dependency>
3.配置类
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
@ConditionalOnProperty(name = "swagger.enbale",havingValue = "true") //是否开启swagger,正式环境一般是需要关闭的(避免不必要的漏洞暴露!)
public class SwaggerConfig {
/**
* swagger2的配置文件
*/
@Bean
public Docket createRestApi() {
List<Parameter> parameters = new ArrayList<>();
parameters.add(new ParameterBuilder()
.name("token")
.description("token")
.required(true)
.modelRef(new ModelRef("string"))
.parameterType("header")
.build());
parameters.add(new ParameterBuilder()
.name("sign")
.description("sign")
.required(true)
.modelRef(new ModelRef("string"))
.parameterType("header")
.build());
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.paths(PathSelectors.any())
.build().globalOperationParameters(parameters)
.securitySchemes(securitySchemes()); //开启Authorize
}
/**
* 构建 api文档的详细信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("xxx平台API")
// 创建人信息
.version("1.0")
// 描述
.description("swagger文档")
.build();
}
private List<ApiKey> securitySchemes() {
List<ApiKey> apiKeyList= new ArrayList();
apiKeyList.add(new ApiKey("token", "token", "header"));
return apiKeyList;
}
}
4.swagger文档路径
访问: http://项目路径/doc.html (如下图,是不是好看多了)