Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add RequestMappingHandlerMapping and ResourceHandlers to a springMVC configuration class

I have a spring MVC configuration class like this:

@Configuration 
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter{
@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver()   {
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/views/");
    resolver.setSuffix(".jsp");
    return resolver;
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations(
            "/resources/");
}


}

I have an issue with maping URLs with a trailing slash,similar to this. so i want to add RequestMappingHandlerMapping class, but based on the instruction i get there i need to extend WebMvcConfigurationSupport class and implement requestMappingHandlerMapping() method, but unfortunatly i have already extended WebMvcConfigurationSupport class for resource's mapping. Is there any way i could add requiest mapping handler to my class?

NOTE: i am using Spring version 3.1.1.RELEASE

like image 341
Techno Avatar asked May 22 '15 17:05

Techno


2 Answers

I didn't get from your question why you can't use WebMvcConfigurationSupport. If by what you mentioned "...unfortunatly i have already extended WebMvcConfigurationSupport class for resource's mapping..." you rather meant that you've already extended the WebMvcConfigurerAdapter, you should be aware that WebMvcConfigurationSupport exposes exactly the same method.

Anyways, following should be a working java config for Spring MVC 3.1 version

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurationSupport {

    @Override
    @Bean
    public RequestMappingHandlerMapping requestMappingHandlerMapping() {
        RequestMappingHandlerMapping hm = super.requestMappingHandlerMapping();
        hm.setUseSuffixPatternMatch(false);
        return hm;
    }

    @Bean
    public InternalResourceViewResolver configureInternalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/**").addResourceLocations(
                "/resources/");
    }
}
like image 194
Master Slave Avatar answered Nov 15 '22 21:11

Master Slave


Overriding requestMappingHandlerMapping using WebMvcConfigurationSupport, may turn off your spring boot's default configurations. A better way could be to use WebMvcRegistrations as,

@Configuration
static class CustomRequestMappingHandlerMapping {

    @Bean
    public WebMvcRegistrationsAdapter webMvcRegistrationsHandlerMapping() {
        return new WebMvcRegistrationsAdapter() {
            @Override
            public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
                return new MyRequestMappingHandlerMapping();
            }
        };
    }
}
like image 45
Paritosh Avatar answered Nov 15 '22 23:11

Paritosh