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,7 +18,7 @@ | ||
| 18 | <!-- 统一依赖管理 --> | 18 | <!-- 统一依赖管理 --> |
| 19 | <spring.boot.version>2.7.7</spring.boot.version> | 19 | <spring.boot.version>2.7.7</spring.boot.version> |
| 20 | <!-- Web 相关 --> | 20 | <!-- Web 相关 --> |
| 21 | - <knife4j.version>3.0.3</knife4j.version> | 21 | + <knife4j.version>4.0.0</knife4j.version> |
| 22 | <swagger-annotations.version>1.6.8</swagger-annotations.version> | 22 | <swagger-annotations.version>1.6.8</swagger-annotations.version> |
| 23 | <servlet.versoin>2.5</servlet.versoin> | 23 | <servlet.versoin>2.5</servlet.versoin> |
| 24 | <!-- DB 相关 --> | 24 | <!-- DB 相关 --> |
| @@ -166,7 +166,7 @@ | @@ -166,7 +166,7 @@ | ||
| 166 | 166 | ||
| 167 | <dependency> | 167 | <dependency> |
| 168 | <groupId>com.github.xiaoymin</groupId> | 168 | <groupId>com.github.xiaoymin</groupId> |
| 169 | - <artifactId>knife4j-spring-boot-starter</artifactId> | 169 | + <artifactId>knife4j-openapi2-spring-boot-starter</artifactId> |
| 170 | <version>${knife4j.version}</version> | 170 | <version>${knife4j.version}</version> |
| 171 | <exclusions> | 171 | <exclusions> |
| 172 | <exclusion> | 172 | <exclusion> |
yudao-framework/yudao-spring-boot-starter-web/pom.xml
| @@ -21,6 +21,12 @@ | @@ -21,6 +21,12 @@ | ||
| 21 | <artifactId>yudao-common</artifactId> | 21 | <artifactId>yudao-common</artifactId> |
| 22 | </dependency> | 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 | <!-- Web 相关 --> | 30 | <!-- Web 相关 --> |
| 25 | <dependency> | 31 | <dependency> |
| 26 | <groupId>org.springframework.boot</groupId> | 32 | <groupId>org.springframework.boot</groupId> |
| @@ -35,7 +41,7 @@ | @@ -35,7 +41,7 @@ | ||
| 35 | 41 | ||
| 36 | <dependency> | 42 | <dependency> |
| 37 | <groupId>com.github.xiaoymin</groupId> | 43 | <groupId>com.github.xiaoymin</groupId> |
| 38 | - <artifactId>knife4j-spring-boot-starter</artifactId> | 44 | + <artifactId>knife4j-openapi2-spring-boot-starter</artifactId> |
| 39 | </dependency> | 45 | </dependency> |
| 40 | <dependency> | 46 | <dependency> |
| 41 | <groupId>io.swagger</groupId> | 47 | <groupId>io.swagger</groupId> |
| @@ -72,6 +78,7 @@ | @@ -72,6 +78,7 @@ | ||
| 72 | <groupId>org.jsoup</groupId> | 78 | <groupId>org.jsoup</groupId> |
| 73 | <artifactId>jsoup</artifactId> | 79 | <artifactId>jsoup</artifactId> |
| 74 | </dependency> | 80 | </dependency> |
| 81 | + | ||
| 75 | </dependencies> | 82 | </dependencies> |
| 76 | 83 | ||
| 77 | </project> | 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,15 +9,16 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties | ||
| 9 | import org.springframework.context.annotation.Bean; | 9 | import org.springframework.context.annotation.Bean; |
| 10 | import org.springframework.http.HttpHeaders; | 10 | import org.springframework.http.HttpHeaders; |
| 11 | import springfox.documentation.builders.ApiInfoBuilder; | 11 | import springfox.documentation.builders.ApiInfoBuilder; |
| 12 | -import springfox.documentation.builders.ExampleBuilder; | 12 | +import springfox.documentation.builders.ParameterBuilder; |
| 13 | import springfox.documentation.builders.PathSelectors; | 13 | import springfox.documentation.builders.PathSelectors; |
| 14 | -import springfox.documentation.builders.RequestParameterBuilder; | 14 | +import springfox.documentation.schema.ModelRef; |
| 15 | import springfox.documentation.service.*; | 15 | import springfox.documentation.service.*; |
| 16 | import springfox.documentation.spi.DocumentationType; | 16 | import springfox.documentation.spi.DocumentationType; |
| 17 | import springfox.documentation.spi.service.contexts.SecurityContext; | 17 | import springfox.documentation.spi.service.contexts.SecurityContext; |
| 18 | import springfox.documentation.spring.web.plugins.Docket; | 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 | import java.util.Collections; | 22 | import java.util.Collections; |
| 22 | import java.util.List; | 23 | import java.util.List; |
| 23 | 24 | ||
| @@ -30,7 +31,7 @@ import static springfox.documentation.builders.RequestHandlerSelectors.basePacka | @@ -30,7 +31,7 @@ import static springfox.documentation.builders.RequestHandlerSelectors.basePacka | ||
| 30 | * @author 芋道源码 | 31 | * @author 芋道源码 |
| 31 | */ | 32 | */ |
| 32 | @AutoConfiguration | 33 | @AutoConfiguration |
| 33 | -@EnableSwagger2 | 34 | +@EnableSwagger2WebMvc |
| 34 | @EnableKnife4j | 35 | @EnableKnife4j |
| 35 | @ConditionalOnClass({Docket.class, ApiInfoBuilder.class}) | 36 | @ConditionalOnClass({Docket.class, ApiInfoBuilder.class}) |
| 36 | // 允许使用 swagger.enable=false 禁用 Swagger | 37 | // 允许使用 swagger.enable=false 禁用 Swagger |
| @@ -59,7 +60,7 @@ public class YudaoSwaggerAutoConfiguration { | @@ -59,7 +60,7 @@ public class YudaoSwaggerAutoConfiguration { | ||
| 59 | .securitySchemes(securitySchemes()) | 60 | .securitySchemes(securitySchemes()) |
| 60 | .securityContexts(securityContexts()) | 61 | .securityContexts(securityContexts()) |
| 61 | // ④ 全局参数(多租户 header) | 62 | // ④ 全局参数(多租户 header) |
| 62 | - .globalRequestParameters(globalRequestParameters()); | 63 | + .globalOperationParameters(globalRequestParameters()); |
| 63 | } | 64 | } |
| 64 | 65 | ||
| 65 | // ========== apiInfo ========== | 66 | // ========== apiInfo ========== |
| @@ -95,7 +96,7 @@ public class YudaoSwaggerAutoConfiguration { | @@ -95,7 +96,7 @@ public class YudaoSwaggerAutoConfiguration { | ||
| 95 | return Collections.singletonList(SecurityContext.builder() | 96 | return Collections.singletonList(SecurityContext.builder() |
| 96 | .securityReferences(securityReferences()) | 97 | .securityReferences(securityReferences()) |
| 97 | // 通过 PathSelectors.regex("^(?!auth).*$"),排除包含 "auth" 的接口不需要使用securitySchemes | 98 | // 通过 PathSelectors.regex("^(?!auth).*$"),排除包含 "auth" 的接口不需要使用securitySchemes |
| 98 | - .operationSelector(o -> o.requestMappingPattern().matches("^(?!auth).*$")) | 99 | + .forPaths(PathSelectors.regex("^(?!auth).*$")) |
| 99 | .build()); | 100 | .build()); |
| 100 | } | 101 | } |
| 101 | 102 | ||
| @@ -109,11 +110,17 @@ public class YudaoSwaggerAutoConfiguration { | @@ -109,11 +110,17 @@ public class YudaoSwaggerAutoConfiguration { | ||
| 109 | 110 | ||
| 110 | // ========== globalRequestParameters ========== | 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,7 +5,6 @@ import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||
| 5 | import org.springframework.beans.BeansException; | 5 | import org.springframework.beans.BeansException; |
| 6 | import org.springframework.beans.factory.config.BeanPostProcessor; | 6 | import org.springframework.beans.factory.config.BeanPostProcessor; |
| 7 | import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; | 7 | import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; |
| 8 | -import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; | ||
| 9 | import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; | 8 | import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; |
| 10 | 9 | ||
| 11 | import java.util.List; | 10 | import java.util.List; |
| @@ -20,7 +19,7 @@ public class SpringFoxHandlerProviderBeanPostProcessor implements BeanPostProces | @@ -20,7 +19,7 @@ public class SpringFoxHandlerProviderBeanPostProcessor implements BeanPostProces | ||
| 20 | 19 | ||
| 21 | @Override | 20 | @Override |
| 22 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { | 21 | public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 23 | - if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { | 22 | + if (bean instanceof WebMvcRequestHandlerProvider) { |
| 24 | customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); | 23 | customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); |
| 25 | } | 24 | } |
| 26 | return bean; | 25 | return bean; |