Spring MVC XML:WebContentInterceptor的PathPatternParser禁用/自定义
Spring Framework 5.3.23 WebContentInterceptor路径匹配报错处理
问题现象
配置WebContentInterceptor时使用路径模式/**/abc,触发如下报错:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'cacheMappings' threw exception; nested exception is org.springframework.web.util.pattern.PatternParseException: No more pattern data allowed after {*...} or ** pattern element
原因分析
Spring 5.3.23中WebContentInterceptor默认使用PathPatternParser处理路径匹配,该解析器的语法规则明确:**作为通配符表示匹配任意层级的路径片段,不允许在**之后再追加其他路径内容,因此/**/abc这种写法违反了语法规范。
同时WebContentInterceptor与PathPatternParser耦合度较高,即便通过PathMatchConfigurer尝试切换为AntPathMatcher,也无法生效。
解决方案
调整路径匹配模式,使其符合PathPatternParser的语法要求:
- 若要匹配所有层级下的
abc路径(如/abc、/x/abc、/x/y/abc等),可使用/**abc或/{*path}/abc({*path}匹配单一层级任意路径,/**匹配多层级) - 若需求是匹配根路径下
abc及其所有子路径,直接使用/abc/**即可
内容的提问来源于stack exchange,提问作者Shobhit
相关产品推荐
相关产品推荐

