AntPattern 问题
No more pattern data allowed after {*…} or ** pattern element
问题描述:从springboot 2.5.15升级到springboot 3.3.0的时候报的一个错误:
Servlet.service() for servlet [dispatcherServlet] threw exception org.springframework.web.util.pattern.PatternParseException: No more >pattern data allowed after {*...} or ** pattern element
之前看到过,一时之间又忘记在哪里看到的了,我的问题就是在匹配路径(SecurityConfig类)时有一段代码:
.requestMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
原因就是Spring Boot 3.0(Spring Framework 6.0)对 URL 路径匹配规则进行了更严格的校验,路径匹配语法更严格,** 通配符后不能再接其他字符,上面的配置在springboot 3.x需要调整为
.requestMatchers(HttpMethod.GET, "/", "/*.html", "/*/*.html", "/*/*.css", "/*/*.js", "/profile/**").permitAll()
解析参数异常问题
问题描述:Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the ‘-parameters’ flag.
即: Spring 框架在尝试解析方法参数时遇到了问题,因为它无法通过反射获取参数的名称信息。
原因:缺少 -parameters 编译选项
默认情况下,Java 编译器不会在生成的字节码中包含方法参数的名称信息。因此,Spring 无法通过反射获取参数名称,导致在解析 @RequestParam、@PathVariable 等注解时出现问题。
注意:在 Java 8 之前,-parameters 选项不可用,因此无法通过编译选项保留参数名称。
解决方案:启用-parameters 编译选项,编译器会将参数名称信息包含在字节码中。
在 pom.xml 中配置 Maven 编译器插件:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>${project.build.sourceEncoding}</encoding>
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
Bean注入问题
问题描述:
Field permitAllUrl in com.ruoyi.framework.config.SecurityConfig >required a single bean, but 2 were found: - requestMappingHandlerMapping: defined by method >'requestMappingHandlerMapping' in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class] - controllerEndpointHandlerMapping: defined by method 'controllerEndpointHandlerMapping' in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/servlet/WebMvcEndpointManagementContextConfiguration.class]
即:Spring 容器中存在两个 Bean,而 Spring 无法确定应该注入哪一个。具体来说,Spring 找到了两个:
- requestMappingHandlerMapping:由 Spring MVC 自动配置提供。
- controllerEndpointHandlerMapping:由 Spring Boot Actuator 提供,用于处理 Actuator 端点。
我的问题:
我在SecurityConfig中有注入:@Autowired private PermitAllUrlProperties permitAllUrl;
PermitAllUrlProperties 类实现了InitializingBean, ApplicationContextAware,其中:
@Override public void afterPropertiesSet(){ RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class); Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods(); //其他业务逻辑 ...... }
解决方案:问题就出现在applicationContext.getBean(RequestMappingHandlerMapping.class)的时候spring容器中存在两个,解决方法是指定bean的名称获取:
@Override public void afterPropertiesSet(){ RequestMappingHandlerMapping mapping = applicationContext.getBean("requestMappingHandlerMapping",RequestMappingHandlerMapping.class); Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods(); //其他业务逻辑 ...... }
Druid问题
问题描述:
Description: Parameter 0 of method removeDruidFilterRegistrationBean in com.ruoyi.framework.config.DruidConfig required a bean of type 'com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties' that could not be found. Action: Consider defining a bean of type 'com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties' in your configuration. Disconnected from the target VM, address: '127.0.0.1:53726', transport: 'socket' Process finished with exit code 0
我的问题是引入的Druid与springboot 3.0不兼容的问题,将Druid的依赖改为下面的即可:
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-3-starter</artifactId> <version>1.2.23</version> </dependency>
java EE 转为 Jakarta EE
将 javax.servlet 依赖改为 jakarta.servlet 后发现有些bean使用的还是java EE的,我遇到的有可能是缓存的问题,使用idea替换后清缓存重启就好了。
依赖版本
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>17</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<spring-boot.version>3.3.0</spring-boot.version>
<druid.version>1.2.23</druid.version>
<bitwalker.version>1.21</bitwalker.version>
<swagger.version>3.0.0</swagger.version>
<kaptcha.version>2.3.3</kaptcha.version>
<pagehelper.boot.version>1.4.7</pagehelper.boot.version>
<fastjson.version>2.0.53</fastjson.version>
<oshi.version>6.6.5</oshi.version>
<commons.io.version>2.13.0</commons.io.version>
<poi.version>4.1.2</poi.version>
<velocity.version>2.3</velocity.version>
<jwt.version>0.9.1</jwt.version>
<tomcat.version>10.1.25</tomcat.version>
<logback.version>1.5.6</logback.version>
<spring-security.version>6.1.0</spring-security.version>
<mybatis-spring-boot.version>3.0.3</mybatis-spring-boot.version>
<mysql.version>8.2.0</mysql.version>
<jaxb-api.version>2.3.1</jaxb-api.version>
<jakarta.version>6.0.0</jakarta.version>
<springdoc.version>2.5.0</springdoc.version>
<spring-aop.version>6.0.13</spring-aop.version>
</properties>