Reputation: 11113
By default, Spring trims leading/trailing whitespace from strings used as path variables. I've tracked this down to be because the trimTokens flag is set to true by default in AntPathMatcher.
What I can't figure out, though, is how to set that flag to false.
Providing my own RequestMappingHandlerMapping bean using an AntPathMatcher where I set it to false didn't work.
How do I change this flag using JavaConfig?
Thanks.
Upvotes: 5
Views: 3162
Reputation: 13910
Just like you pointed out the issue is because all versions of the Spring Framework before 4.3.0 have the default antPathMatcher with the trimTokens flag is set to true.
Add a config file that returns the default antPathMatcher but with the trimTokens flag set to 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());
}
}
Upvotes: 1
Reputation: 124526
Let your configuration extend WebMvcConfigurationSupport
override requestMappingHandlerMapping()
and configure accordingly.
@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;
}
}
Upvotes: 4