You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring WebFlux函数式路由下WebFluxConfigurer大小写匹配配置失效问题

问题根因

WebFluxConfigurer.configurePathMatching() 提供的大小写不敏感配置,仅对注解式路由(@Controller、@RestController 声明的接口)生效,对函数式声明的RouterFunction路由体系不生效,两套路由的路径匹配逻辑是独立实现的,因此你现有的配置对函数式路由不起作用。


解决方案

方案1:全局配置函数式路由大小写不敏感(推荐)

注入自定义的RouterFunctionMapping实例,修改全局路径匹配规则,不需要改动现有路由定义,所有函数式路由自动生效:

@Configuration
class RouterFunctionConfig {
    @Bean
    fun routerFunctionMapping(routerFunctions: List<RouterFunction<*>>): RouterFunctionMapping {
        val pathPatternParser = PathPatternParser()
        // 关闭路径匹配大小写校验
        pathPatternParser.isCaseSensitive = false
        val mapping = RouterFunctionMapping(routerFunctions)
        mapping.setPathPatternParser(pathPatternParser)
        return mapping
    }
}

方案2:按需为指定路由配置大小写不敏感

如果只需要部分路由支持大小写不敏感,不需要全局修改,可以在路由路径中加正则标记(?i)实现:

@Configuration
class UserRoutes {
    @Bean
    fun userRouterFunction(userHandler: UserHandler) = coRouter {
        // (?i:xxx) 表示xxx段路径不区分大小写
        "/(?i:users)".nest {
            GET("/(?i:all)", userHandler::getAllUser)
        }
    }
}

注意事项

如果你的项目同时使用注解式路由和函数式路由,原有WebConfig中的configurePathMatching配置不需要删除,两套配置各自独立生效。

内容的提问来源于stack exchange,提问作者Maxiepaxie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 12:39:03