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
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();
}
};
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With