Spring Boot迁移后配置ant_path_matcher不生效问题求助
问题:Spring Boot 2.5迁移至3.0时路径匹配配置不生效导致启动失败
背景
正在将Spring Boot从2.5版本迁移至3.4版本(当前处于3.0版本),由于新版本中ant_path_matcher不再是默认路径匹配策略,因此在application.yaml中添加了如下配置:
spring: mvc: pathmatch: matching-strategy: ant_path_matcher
该配置此前多次解决过同类问题,但本次报错仍持续存在。
WebMvcConfig配置
以下是WebMvcConfig.kt文件内容:
package our.application.path.configuration import com.fasterxml.jackson.databind.ObjectMapper import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter import org.springframework.web.cors.CorsConfiguration import org.springframework.web.cors.CorsConfigurationSource import org.springframework.web.cors.UrlBasedCorsConfigurationSource import org.springframework.web.servlet.config.annotation.EnableWebMvc import org.springframework.web.servlet.config.annotation.ViewControllerRegistry import org.springframework.web.servlet.config.annotation.WebMvcConfigurer @Configuration @EnableWebMvc class WebMvcConfig : WebMvcConfigurer { companion object { // Forwarding target const val FORWARD_INDEX_HTML = "forward:/index.html" } override fun addViewControllers(registry: ViewControllerRegistry) { // Map "/" to /index.html registry.addViewController("/").setViewName(FORWARD_INDEX_HTML) // Single directory level - no need to exclude "api" registry.addViewController("/{x:[\\w\\-]+}").setViewName(FORWARD_INDEX_HTML) // Multi-level directory path, need to exclude "api" on the first part of the path registry.addViewController("/{x:^(?!api$).*$}/**/{y:[\\w\\-]+}").setViewName(FORWARD_INDEX_HTML) } @Bean fun corsConfigurationSource(): CorsConfigurationSource { val source = UrlBasedCorsConfigurationSource() source.registerCorsConfiguration("/**", CorsConfiguration().applyPermitDefaultValues()) return source } @Bean open fun mappingJackson2HttpMessageConverter(objectMapper: ObjectMapper): MappingJackson2HttpMessageConverter { return MappingJackson2HttpMessageConverter(objectMapper) } }
启动错误日志
无论是否添加上述yaml配置,应用启动末尾都会出现如下错误:
2025-05-22 16:56:15.229 INFO 29128 --- [ main] s.w.a.s.ProblemSecurityBeanPostProcessor [] : Register HttpSecurity's exceptionHandling 2025-05-22 16:56:15.289 WARN 29128 --- [ main] ConfigServletWebServerApplicationContext [] : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'publicApiFilterChain' defined in class path resource [our/application/path/configuration/SecurityConfig.class]: Unsatisfied dependency expressed through method 'publicApiFilterChain' parameter 1: Error creating bean with name 'mvc' defined in class path resource [our/application/path/configuration/SecurityConfig.class]: Unsatisfied dependency expressed through method 'mvc' parameter 0: Error creating bean with name 'mvcHandlerMappingIntrospector' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Error creating bean with name 'viewControllerHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: No more pattern data allowed after {*...} or ** pattern element 2025-05-22 16:56:15.290 INFO 29128 --- [ main] j.LocalContainerEntityManagerFactoryBean [] : Closing JPA EntityManagerFactory for persistence unit 'default' 2025-05-22 16:56:15.291 INFO 29128 --- [ main] com.zaxxer.hikari.HikariDataSource [] : HikariPool-1 - Shutdown initiated... 2025-05-22 16:56:15.304 INFO 29128 --- [ main] com.zaxxer.hikari.HikariDataSource [] : HikariPool-1 - Shutdown completed. 2025-05-22 16:56:15.306 INFO 29128 --- [ main] o.apache.catalina.core.StandardService [] : Stopping service [Tomcat] 2025-05-22 16:56:15.319 INFO 29128 --- [ main] .s.b.a.l.ConditionEvaluationReportLogger [] : Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled. 2025-05-22 16:56:15.332 ERROR 29128 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter [] : *************************** APPLICATION FAILED TO START *************************** Description: Invalid mapping pattern detected: /{x:^(?!api$).*$}/**/{y:[\w\-]+} ^ No more pattern data allowed after {*...} or ** pattern element Action: Fix this pattern in your application or switch to the legacy parser implementation with 'spring.mvc.pathmatch.matching-strategy=ant_path_matcher'. Process finished with exit code 1
已尝试的操作
- 在Google和Stack Overflow上搜索问题
- 咨询AI(ChatGPT)
- 多次对比其他正常运行项目的配置
- 询问多位同事
如需更多信息,请告知。
内容的提问来源于stack exchange,提问作者Felix Conrad
相关产品推荐
相关产品推荐

