简体   繁体   English

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

[英]Disable trimming of whitespace from path variables in Spring 3.2

By default, Spring trims leading/trailing whitespace from strings used as path variables. 默认情况下,Spring从用作路径变量的字符串中修剪前导/尾随空格。 I've tracked this down to be because the trimTokens flag is set to true by default in AntPathMatcher . 我已经跟踪下来是因为trimTokens标志被AntPathMatcher设置为true,默认情况下。

What I can't figure out, though, is how to set that flag to false . 不过,我不知道的是如何将该标志设置为false

Providing my own RequestMappingHandlerMapping bean using an AntPathMatcher where I set it to false didn't work. 使用将其设置为falseAntPathMatcher提供我自己的RequestMappingHandlerMapping bean无效。

How do I change this flag using JavaConfig? 如何使用JavaConfig更改此标志?

Thanks. 谢谢。

Let your configuration extend WebMvcConfigurationSupport override requestMappingHandlerMapping() and configure accordingly. 让您的配置扩展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;
    }
} 

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. 就像您指出的那样,问题是因为4.3.0之前的所有版本的Spring Framework都有默认的antPathMatcher ,且trimTokens标志设置为true。


Solution

Add a config file that returns the default antPathMatcher but with the trimTokens flag set to false 添加一个配置文件,该配置文件返回默认的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());
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM