문제

I have a Spring MVC 3.2.8 application with Java configuration. I want to disable url decoding as I have this issue with / in the uri. Spring 3.2.8 should fix this.

The problem is, I can't set Url decoding to false in RequestMappingHandlerMapping. I tried to override it with:

@Configuration
public class MobileWebPublicConfig extends WebMvcConfigurationSupport {

    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping handlerMapping = super.requestMappingHandlerMapping();
        handlerMapping.setUrlDecode(false);
        return handlerMapping;
    }
}

But when I do this, it breaks my application an auto-wiring stops working.

What am I missing?

UPDATE: container is Tomcat 6, there is no stacktrace related, the app fails when trying to access an autowired element due to it being null. Commenting the configuration above makes it work fine.

도움이 되었습니까?

해결책

The documentation for @EnableWebMvc describes three levels of customization for tailoring your configuration. You can:

  1. Use @EnableWebMvc on its own to import the standard configuration.
  2. Use @EnableWebMvc with configuration classes that extend WebMvcConfigurerAdapter; the subclasses will have their implemented methods called automatically to apply your changes to the standard configuration.
  3. Don't use @EnableWebMvc, but instead make your configuration class extend WebMvcConfigurationSupport to gain full control over your configuration.

However, what the documentation does not make clear is that if you go for option 3 it will not automatically apply the methods implemented in classes that extend WebMvcConfigurerAdapter.

As others have said, we really need to see your full configuration, but if simply extending WebMvcConfigurationSupport and removing @EnableWebMvc breaks other parts of your configuration, my guess is that you are still using WebMvcConfigurerAdapter somewhere else in your config.

If this is the case, you have two options:

  1. Rewrite all your configuration that uses WebMvcConfigurerAdapter and move the code into a single configuration class that extends WebMvcConfigurationSupport.
  2. Extend DelegatingWebMvcConfiguration instead of WebMvcConfigurationSupport. This will give you all the standard configuration behaviour that you normally get when using @EnableWebMvc but still allow you to override methods as required. Note that in any method you override, you'll also need to call the equivalent method in super() if you want to keep the default behaviour too.

다른 팁

EDIT: Also make sure that you don't have @EnableWebMvc annotation somewhere since this annotation will import the original WebMvcConfigurationSupport and not the extended version.

WebMvcConfigurationSupport javadoc says

It is typically imported by adding @EnableWebMvc to an application @Configuration class. An alternative more advanced option is to extend directly from this class and override methods as necessary remembering to add @Configuration to the subclass and @Bean to overridden @Bean methods.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top