how to add RequestMappingHandlerMapping and ResourceHandlers to a springMVC configuration class
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/");
}
}
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();
}
};
}
}
And since Spring Boot 2.0.0 you could use WebMvcRegistrations directly
@Configuration
public class WebMvcConfig {
@Bean
public WebMvcRegistrations webMvcRegistrationsHandlerMapping() {
return new WebMvcRegistrations() {
@Override
public RequestMappingHandlerMapping getRequestMappingHandlerMapping() {
return new CustomRequestMappingHandlerMapping();
}
};
}
}