Commit b4b23596a555b41bc5d3ada0d6a6c6883ebed2e8
1 parent
55db35e3
feat: 升级 knife4j 4.0
Showing
4 changed files
with
29 additions
and
16 deletions
yudao-dependencies/pom.xml
| ... | ... | @@ -18,7 +18,7 @@ |
| 18 | 18 | <!-- 统一依赖管理 --> |
| 19 | 19 | <spring.boot.version>2.7.7</spring.boot.version> |
| 20 | 20 | <!-- Web 相关 --> |
| 21 | - <knife4j.version>3.0.3</knife4j.version> | |
| 21 | + <knife4j.version>4.0.0</knife4j.version> | |
| 22 | 22 | <swagger-annotations.version>1.6.8</swagger-annotations.version> |
| 23 | 23 | <servlet.versoin>2.5</servlet.versoin> |
| 24 | 24 | <!-- DB 相关 --> |
| ... | ... | @@ -166,7 +166,7 @@ |
| 166 | 166 | |
| 167 | 167 | <dependency> |
| 168 | 168 | <groupId>com.github.xiaoymin</groupId> |
| 169 | - <artifactId>knife4j-spring-boot-starter</artifactId> | |
| 169 | + <artifactId>knife4j-openapi2-spring-boot-starter</artifactId> | |
| 170 | 170 | <version>${knife4j.version}</version> |
| 171 | 171 | <exclusions> |
| 172 | 172 | <exclusion> | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/pom.xml
| ... | ... | @@ -21,6 +21,12 @@ |
| 21 | 21 | <artifactId>yudao-common</artifactId> |
| 22 | 22 | </dependency> |
| 23 | 23 | |
| 24 | + <dependency> | |
| 25 | + <groupId>org.apache.commons</groupId> | |
| 26 | + <artifactId>commons-lang3</artifactId> | |
| 27 | + <scope>provided</scope> | |
| 28 | + </dependency> | |
| 29 | + | |
| 24 | 30 | <!-- Web 相关 --> |
| 25 | 31 | <dependency> |
| 26 | 32 | <groupId>org.springframework.boot</groupId> |
| ... | ... | @@ -35,7 +41,7 @@ |
| 35 | 41 | |
| 36 | 42 | <dependency> |
| 37 | 43 | <groupId>com.github.xiaoymin</groupId> |
| 38 | - <artifactId>knife4j-spring-boot-starter</artifactId> | |
| 44 | + <artifactId>knife4j-openapi2-spring-boot-starter</artifactId> | |
| 39 | 45 | </dependency> |
| 40 | 46 | <dependency> |
| 41 | 47 | <groupId>io.swagger</groupId> |
| ... | ... | @@ -72,6 +78,7 @@ |
| 72 | 78 | <groupId>org.jsoup</groupId> |
| 73 | 79 | <artifactId>jsoup</artifactId> |
| 74 | 80 | </dependency> |
| 81 | + | |
| 75 | 82 | </dependencies> |
| 76 | 83 | |
| 77 | 84 | </project> | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/swagger/config/YudaoSwaggerAutoConfiguration.java
| ... | ... | @@ -9,15 +9,16 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties |
| 9 | 9 | import org.springframework.context.annotation.Bean; |
| 10 | 10 | import org.springframework.http.HttpHeaders; |
| 11 | 11 | import springfox.documentation.builders.ApiInfoBuilder; |
| 12 | -import springfox.documentation.builders.ExampleBuilder; | |
| 12 | +import springfox.documentation.builders.ParameterBuilder; | |
| 13 | 13 | import springfox.documentation.builders.PathSelectors; |
| 14 | -import springfox.documentation.builders.RequestParameterBuilder; | |
| 14 | +import springfox.documentation.schema.ModelRef; | |
| 15 | 15 | import springfox.documentation.service.*; |
| 16 | 16 | import springfox.documentation.spi.DocumentationType; |
| 17 | 17 | import springfox.documentation.spi.service.contexts.SecurityContext; |
| 18 | 18 | import springfox.documentation.spring.web.plugins.Docket; |
| 19 | -import springfox.documentation.swagger2.annotations.EnableSwagger2; | |
| 19 | +import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc; | |
| 20 | 20 | |
| 21 | +import java.util.ArrayList; | |
| 21 | 22 | import java.util.Collections; |
| 22 | 23 | import java.util.List; |
| 23 | 24 | |
| ... | ... | @@ -30,7 +31,7 @@ import static springfox.documentation.builders.RequestHandlerSelectors.basePacka |
| 30 | 31 | * @author 芋道源码 |
| 31 | 32 | */ |
| 32 | 33 | @AutoConfiguration |
| 33 | -@EnableSwagger2 | |
| 34 | +@EnableSwagger2WebMvc | |
| 34 | 35 | @EnableKnife4j |
| 35 | 36 | @ConditionalOnClass({Docket.class, ApiInfoBuilder.class}) |
| 36 | 37 | // 允许使用 swagger.enable=false 禁用 Swagger |
| ... | ... | @@ -59,7 +60,7 @@ public class YudaoSwaggerAutoConfiguration { |
| 59 | 60 | .securitySchemes(securitySchemes()) |
| 60 | 61 | .securityContexts(securityContexts()) |
| 61 | 62 | // ④ 全局参数(多租户 header) |
| 62 | - .globalRequestParameters(globalRequestParameters()); | |
| 63 | + .globalOperationParameters(globalRequestParameters()); | |
| 63 | 64 | } |
| 64 | 65 | |
| 65 | 66 | // ========== apiInfo ========== |
| ... | ... | @@ -95,7 +96,7 @@ public class YudaoSwaggerAutoConfiguration { |
| 95 | 96 | return Collections.singletonList(SecurityContext.builder() |
| 96 | 97 | .securityReferences(securityReferences()) |
| 97 | 98 | // 通过 PathSelectors.regex("^(?!auth).*$"),排除包含 "auth" 的接口不需要使用securitySchemes |
| 98 | - .operationSelector(o -> o.requestMappingPattern().matches("^(?!auth).*$")) | |
| 99 | + .forPaths(PathSelectors.regex("^(?!auth).*$")) | |
| 99 | 100 | .build()); |
| 100 | 101 | } |
| 101 | 102 | |
| ... | ... | @@ -109,11 +110,17 @@ public class YudaoSwaggerAutoConfiguration { |
| 109 | 110 | |
| 110 | 111 | // ========== globalRequestParameters ========== |
| 111 | 112 | |
| 112 | - private static List<RequestParameter> globalRequestParameters() { | |
| 113 | - RequestParameterBuilder tenantParameter = new RequestParameterBuilder() | |
| 114 | - .name(HEADER_TENANT_ID).description("租户编号") | |
| 115 | - .in(ParameterType.HEADER).example(new ExampleBuilder().value(1L).build()); | |
| 116 | - return Collections.singletonList(tenantParameter.build()); | |
| 113 | + private static List<Parameter> globalRequestParameters() { | |
| 114 | + List<Parameter> tenantParameter = new ArrayList<>(); | |
| 115 | + tenantParameter.add(new ParameterBuilder() | |
| 116 | + .name(HEADER_TENANT_ID) | |
| 117 | + .description("租户编号") | |
| 118 | + .modelRef(new ModelRef("long")) | |
| 119 | + .defaultValue("1") | |
| 120 | + .parameterType("header") | |
| 121 | + .required(true) | |
| 122 | + .build()); | |
| 123 | + return tenantParameter; | |
| 117 | 124 | } |
| 118 | 125 | |
| 119 | 126 | } | ... | ... |
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/swagger/core/SpringFoxHandlerProviderBeanPostProcessor.java
| ... | ... | @@ -5,7 +5,6 @@ import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 5 | 5 | import org.springframework.beans.BeansException; |
| 6 | 6 | import org.springframework.beans.factory.config.BeanPostProcessor; |
| 7 | 7 | import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; |
| 8 | -import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; | |
| 9 | 8 | import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; |
| 10 | 9 | |
| 11 | 10 | import java.util.List; |
| ... | ... | @@ -20,7 +19,7 @@ public class SpringFoxHandlerProviderBeanPostProcessor implements BeanPostProces |
| 20 | 19 | |
| 21 | 20 | @Override |
| 22 | 21 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 23 | - if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { | |
| 22 | + if (bean instanceof WebMvcRequestHandlerProvider) { | |
| 24 | 23 | customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); |
| 25 | 24 | } |
| 26 | 25 | return bean; | ... | ... |