From 47270ad37fda01624be018411cb7cc8136443a97 Mon Sep 17 00:00:00 2001 From: qxo <49526356@qq.com> Date: Sun, 19 Feb 2023 22:33:45 +0800 Subject: [PATCH] feat: spring boot to 2.7.x --- pom.xml | 2 +- ruoyi-admin/pom.xml | 4 +- .../web/core/config/SpringfoxFixConfig.java | 60 +++++++++++++++++++ .../src/main/resources/application.yml | 2 + 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SpringfoxFixConfig.java diff --git a/pom.xml b/pom.xml index d9ed7362d..99c08d5c2 100644 --- a/pom.xml +++ b/pom.xml @@ -40,7 +40,7 @@ org.springframework.boot spring-boot-dependencies - 2.5.14 + 2.7.8 pom import diff --git a/ruoyi-admin/pom.xml b/ruoyi-admin/pom.xml index 30996c790..740e2390a 100644 --- a/ruoyi-admin/pom.xml +++ b/ruoyi-admin/pom.xml @@ -45,8 +45,8 @@ - mysql - mysql-connector-java + com.mysql + mysql-connector-j diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SpringfoxFixConfig.java b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SpringfoxFixConfig.java new file mode 100644 index 000000000..5e71c0586 --- /dev/null +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/core/config/SpringfoxFixConfig.java @@ -0,0 +1,60 @@ +package com.ruoyi.web.core.config; + +import java.lang.reflect.Field; +import java.util.List; +import java.util.stream.Collectors; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.ReflectionUtils; +import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping; + +import springfox.documentation.spring.web.plugins.WebFluxRequestHandlerProvider; +import springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider; + +/** + * Springfox 在springboot 2.6+的处理. + * + * 还要设置spring.mvc.pathmatch.matching-strategy=ant_path_matcher才行. + * + * ref: https://blog.csdn.net/kkorkk/article/details/123774484 + */ +@Configuration(proxyBeanMethods = false) +public class SpringfoxFixConfig { + + @Bean + //-Druoyi.fix.springfox=false for spring < 2.6 + @ConditionalOnProperty(prefix = "ruoyi.fix", name = "springfox", havingValue = "true", matchIfMissing = true) + public BeanPostProcessor springfoxHandlerProviderBeanPostProcessor() { + return new BeanPostProcessor() { + @Override + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + if (bean instanceof WebMvcRequestHandlerProvider || bean instanceof WebFluxRequestHandlerProvider) { + customizeSpringfoxHandlerMappings(getHandlerMappings(bean)); + } + return bean; + } + + private void customizeSpringfoxHandlerMappings(List mappings) { + List copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null) + .collect(Collectors.toList()); + mappings.clear(); + mappings.addAll(copy); + } + + @SuppressWarnings("unchecked") + private List getHandlerMappings(Object bean) { + try { + Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings"); + field.setAccessible(true); + return (List) field.get(bean); + } catch (IllegalArgumentException | IllegalAccessException e) { + throw new IllegalStateException(e); + } + } + }; + } +} diff --git a/ruoyi-admin/src/main/resources/application.yml b/ruoyi-admin/src/main/resources/application.yml index d99251661..0074ffa5c 100644 --- a/ruoyi-admin/src/main/resources/application.yml +++ b/ruoyi-admin/src/main/resources/application.yml @@ -140,3 +140,5 @@ xss: swagger: # 是否开启swagger enabled: true + +spring.mvc.pathmatch.matching-strategy: ant_path_matcher