Java 在Spring 3.2中禁用从路径变量中修剪空白

Java 在Spring 3.2中禁用从路径变量中修剪空白,java,spring,spring-mvc,Java,Spring,Spring Mvc,默认情况下,Spring会从用作路径变量的字符串中修剪前导/尾随空格。我追踪到这一点是因为在AntPathMatcher中,trimTokens标志默认设置为true 但是,我不知道如何将该标志设置为false 使用AntPathMatcher提供我自己的RequestMappingHandlerMappingbean,并将其设置为false,但这不起作用 如何使用JavaConfig更改此标志 谢谢。让您的配置扩展WebMvcConfigurationSupport覆盖requestMappi

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

但是,我不知道如何将该标志设置为false

使用AntPathMatcher提供我自己的RequestMappingHandlerMappingbean,并将其设置为false,但这不起作用

如何使用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;
    }
} 
问题 正如您指出的,这个问题是因为所有带有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());
  }
}

你能提供一个示例URL吗?您是否对URL进行了编码(
[space]
-->
%20
)?很好,谢谢!我在扩展WebMVCConfigureAdapter,而不是WebMvcConfigurationSupport。从spring 4.0.3开始,您还可以覆盖
configurePathMatch()
以设置路径匹配器。