禁用在Spring 3.2中修剪路径变量中的空格

默认情况下,Spring会从用作路径变量的字符串中修剪前导/尾随空格。 我跟踪这个是因为默认情况下在AntPathMatcher中 trimTokens标志设置为true

但是,我无法弄清楚的是如何将该标志设置为false

使用我将其设置为falseAntPathMatcher提供我自己的RequestMappingHandlerMapping bean不起作用。

如何使用JavaConfig更改此标志?

谢谢。

让您的配置扩展WebMvcConfigurationSupport覆盖requestMappingHandlerMapping()并进行相应配置。

 @Configuration public MyConfig extends WebMvcConfigurationSupport { @Bean public PathMatcher pathMatcher() { // Your AntPathMatcher here. } @Bean public RequestMappingHandlerMapping requestMappingHandlerMapping() { RequestMappingHandlerMapping rmhm = super.requestMappingHandlerMapping(); rmhm.setPathMatcher(pathMatcher()); return rmhm; } } 

问题

就像你指出的那样,问题是因为4.3.0之前的所有Spring Framework版本都有默认的antPathMatcher ,其trimTokens标志设置为true。

添加一个配置文件,该文件返回默认的antPathMatcher,但trimTokens标志设置为false

 @Configuration @EnableAspectJAutoProxy public class PricingConfig extends WebMvcConfigurerAdapter { @Bean public PathMatcher pathMatcher() { AntPathMatcher pathMatcher = new AntPathMatcher(); pathMatcher.setTrimTokens(false); return pathMatcher; } @Override public void configurePathMatch(PathMatchConfigurer configurer) { configurer.setPathMatcher(pathMatcher()); } }