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">
<parent>
<artifactId>ecloud-common</artifactId>
<groupId>com.ecloud</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ecloud-common-swagger</artifactId>
<dependencies>
<!-- SpringBoot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Swagger 依赖配置 -->
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger.core.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger.core.version}</version>
</dependency>
<!-- Swagger -->
<!-- 3.0版本只需要引入一个,不再像2.x版本需要引入两个包 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
</project>Swagger 配置类
package com.ecloud.common.swagger.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import java.util.*;
/**
* swagger 配置
*
* @Author Skwax
* @Date 2021/07/23 8:53
* @Version 1.0
*/
@Configuration
public class SwaggerConfig {
@Value("${swagger.enable:true}")
private Boolean enable;
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
//资源
.globalResponses(HttpMethod.GET, new ArrayList<>())
.globalResponses(HttpMethod.PUT, new ArrayList<>())
.globalResponses(HttpMethod.POST, new ArrayList<>())
.globalResponses(HttpMethod.DELETE, new ArrayList<>())
//是否启动
.enable(enable)
//头部信息
.apiInfo(apiInfo())
.select()
/**
* RequestHandlerSelectors,配置要扫描接口的方式
* basePackage指定要扫描的包
* any()扫描所有,项目中的所有接口都会被扫描到
* none()不扫描
* withClassAnnotation()扫描类上的注解
* withMethodAnnotation()扫描方法上的注解
*/
.apis(RequestHandlerSelectors.any())
//过滤某个路径
.paths(PathSelectors.any())
.build()
//协议
.protocols(newHashSet("https", "http"))
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}
/**
* API 页面上半部分展示信息
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("接口文档")
.description("@author etiersoft")
.contact(new Contact("etiersoft", null, "etiersoft@etiersoft.com"))
.version("1.0")
.build();
}
/**
* 设置接口单独的授权信息
*/
private List<SecurityScheme> securitySchemes() {
return Collections.singletonList(new ApiKey("access_token", "access_token", "header"));
}
/**
* 授权信息全局应用
*/
private List<SecurityContext> securityContexts() {
return Collections.singletonList(
SecurityContext.builder()
.securityReferences(
Collections.singletonList(new SecurityReference("access_token",
new AuthorizationScope[]{new AuthorizationScope("global", "")}
)))
//.forPaths(PathSelectors.any())
.build()
);
}
@SafeVarargs
private final <T> Set<T> newHashSet(T... ts) {
if (ts.length > 0) {
return new LinkedHashSet<>(Arrays.asList(ts));
}
return null;
}
}
spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.ecloud.common.swagger.config.SwaggerConfig
评论