默认情况下,Spring会从用作路径变量的字符串中修剪前导/尾随空格。我跟踪这是因为 trimTokens 标志默认设置为 true AntPathMatcher 。
但我无法弄清楚如何将该标志设置为 false 。
使用 AntPathMatcher 提供我自己的 RequestMappingHandlerMapping bean,我将其设置为 false 不起作用。
如何使用JavaConfig更改此标志?
感谢。
答案 0 :(得分:4)
让您的配置扩展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;
}
}
答案 1 :(得分:1)
就像你指出的那样,问题是因为带有 trimTokens 标志的Spring Framework before 4.3.0 have the default antPathMatcher的所有版本都设置为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());
}
}