feat: 升级knife4j 4.0

This commit is contained in:
xingyu 2023-01-17 15:30:30 +08:00
parent 67dd42dd16
commit 4f43bc2d63
4 changed files with 22 additions and 16 deletions

View File

@ -20,7 +20,7 @@
<spring.cloud.version>2021.0.5</spring.cloud.version> <spring.cloud.version>2021.0.5</spring.cloud.version>
<spring.cloud.alibaba.version>2021.0.4.0</spring.cloud.alibaba.version> <spring.cloud.alibaba.version>2021.0.4.0</spring.cloud.alibaba.version>
<!-- Web 相关 --> <!-- Web 相关 -->
<knife4j.version>3.0.3</knife4j.version> <knife4j.version>4.0.0</knife4j.version>
<swagger-annotations.version>1.6.8</swagger-annotations.version> <swagger-annotations.version>1.6.8</swagger-annotations.version>
<servlet.versoin>2.5</servlet.versoin> <servlet.versoin>2.5</servlet.versoin>
<!-- DB 相关 --> <!-- DB 相关 -->
@ -191,7 +191,7 @@
<dependency> <dependency>
<groupId>com.github.xiaoymin</groupId> <groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId> <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
<version>${knife4j.version}</version> <version>${knife4j.version}</version>
<exclusions> <exclusions>
<exclusion> <exclusion>

View File

@ -35,7 +35,7 @@
<dependency> <dependency>
<groupId>com.github.xiaoymin</groupId> <groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId> <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.swagger</groupId> <groupId>io.swagger</groupId>

View File

@ -7,18 +7,18 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ExampleBuilder; import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestParameterBuilder; import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.*; import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext; import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -31,7 +31,7 @@ import static springfox.documentation.builders.RequestHandlerSelectors.basePacka
* @author 芋道源码 * @author 芋道源码
*/ */
@AutoConfiguration @AutoConfiguration
@EnableSwagger2 @EnableSwagger2WebMvc
@EnableKnife4j @EnableKnife4j
@ConditionalOnClass({Docket.class, ApiInfoBuilder.class}) @ConditionalOnClass({Docket.class, ApiInfoBuilder.class})
// 允许使用 swagger.enable=false 禁用 Swagger // 允许使用 swagger.enable=false 禁用 Swagger
@ -60,7 +60,7 @@ public class YudaoSwaggerAutoConfiguration {
.securitySchemes(securitySchemes()) .securitySchemes(securitySchemes())
.securityContexts(securityContexts()) .securityContexts(securityContexts())
// 全局参数多租户 header // 全局参数多租户 header
.globalRequestParameters(globalRequestParameters()); .globalOperationParameters(globalRequestParameters());
} }
// ========== apiInfo ========== // ========== apiInfo ==========
@ -96,7 +96,7 @@ public class YudaoSwaggerAutoConfiguration {
return Collections.singletonList(SecurityContext.builder() return Collections.singletonList(SecurityContext.builder()
.securityReferences(securityReferences()) .securityReferences(securityReferences())
// 通过 PathSelectors.regex("^(?!auth).*$")排除包含 "auth" 的接口不需要使用securitySchemes // 通过 PathSelectors.regex("^(?!auth).*$")排除包含 "auth" 的接口不需要使用securitySchemes
.operationSelector(o -> o.requestMappingPattern().matches("^(?!auth).*$")) .forPaths(PathSelectors.regex("^(?!auth).*$"))
.build()); .build());
} }
@ -110,11 +110,17 @@ public class YudaoSwaggerAutoConfiguration {
// ========== globalRequestParameters ========== // ========== globalRequestParameters ==========
private static List<RequestParameter> globalRequestParameters() { private static List<Parameter> globalRequestParameters() {
RequestParameterBuilder tenantParameter = new RequestParameterBuilder() List<Parameter> tenantParameter = new ArrayList<>();
.name(HEADER_TENANT_ID).description("租户编号") tenantParameter.add(new ParameterBuilder()
.in(ParameterType.HEADER).example(new ExampleBuilder().value(1L).build()); .name(HEADER_TENANT_ID)
return Collections.singletonList(tenantParameter.build()); .description("租户编号")
.modelRef(new ModelRef("long"))
.defaultValue("1")
.parameterType("header")
.required(true)
.build());
return tenantParameter;
} }
} }

View File

@ -46,7 +46,7 @@
<dependency> <dependency>
<groupId>com.github.xiaoymin</groupId> <groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId> <artifactId>knife4j-openapi2-spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.swagger</groupId> <groupId>io.swagger</groupId>