복붙노트

[SPRING] Spring 3.2에서 경로 변수의 공백을 자르지 않는다.

SPRING

Spring 3.2에서 경로 변수의 공백을 자르지 않는다.

기본적으로 Spring은 경로 변수로 사용 된 문자열에서 선행 / 후행 공백을 제거합니다. trimTokens 플래그가 AntPathMatcher에서 기본적으로 true로 설정되어 있기 때문에 이것을 추적했습니다.

그러나 내가 알 수없는 것은 플래그를 false로 설정하는 방법입니다.

AntPathMatcher를 사용하여 내 자신의 RequestMappingHandlerMapping 빈을 제공하면 false로 설정되어 작동하지 않습니다.

JavaConfig를 사용하여이 플래그를 어떻게 변경합니까?

감사.

해결법

  1. ==============================

    1.구성에서 WebMvcConfigurationSupport를 확장하여 requestMappingHandlerMapping ()을 대체하고 그에 따라 구성하십시오.

    구성에서 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;
        }
    } 
    
  2. ==============================

    2.단지 4.3.0 이전의 스프링 프레임 워크의 모든 버전은 trimTokens 플래그가 true로 설정된 기본 antPathMatcher를 가지고 있기 때문에 문제가 지적했듯이.

    단지 4.3.0 이전의 스프링 프레임 워크의 모든 버전은 trimTokens 플래그가 true로 설정된 기본 antPathMatcher를 가지고 있기 때문에 문제가 지적했듯이.

    trimTokens 플래그가 false로 설정된 상태에서 기본 antPathMatcher를 반환하는 구성 파일을 추가합니다.

    @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());
      }
    }
    
  3. from https://stackoverflow.com/questions/21047104/disable-trimming-of-whitespace-from-path-variables-in-spring-3-2 by cc-by-sa and MIT license