Skip to content

Commit

Permalink
feat: support spring 2.7.x
Browse files Browse the repository at this point in the history
  • Loading branch information
qxo committed Feb 19, 2023
1 parent 9d02f8f commit e8218a8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.14</version>
<version>2.7.8</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
4 changes: 2 additions & 2 deletions ruoyi-admin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@

<!-- Mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>

<!-- 核心模块-->
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <T extends RequestMappingInfoHandlerMapping> void customizeSpringfoxHandlerMappings(List<T> mappings) {
List<T> copy = mappings.stream().filter(mapping -> mapping.getPatternParser() == null)
.collect(Collectors.toList());
mappings.clear();
mappings.addAll(copy);
}

@SuppressWarnings("unchecked")
private List<RequestMappingInfoHandlerMapping> getHandlerMappings(Object bean) {
try {
Field field = ReflectionUtils.findField(bean.getClass(), "handlerMappings");
field.setAccessible(true);
return (List<RequestMappingInfoHandlerMapping>) field.get(bean);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
};
}
}
2 changes: 2 additions & 0 deletions ruoyi-admin/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,5 @@ xss:
swagger:
# 是否开启swagger
enabled: true

spring.mvc.pathmatch.matching-strategy: ant_path_matcher

0 comments on commit e8218a8

Please sign in to comment.