Raniz
Raniz

Reputation: 11113

Disable trimming of whitespace from path variables in Spring 3.2

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

Answers (2)

Eddie Martinez
Eddie Martinez

Reputation: 13910

The problem

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.


Solution

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

M. Deinum
M. Deinum

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

Related Questions