In this example, we will understand the effect of using method setUseRegisteredSuffixPatternMatch() of RequestMappingHandlerMapping . This flag specifies whether to use suffix pattern match (".*"). The default value is true, that means a handler with /xyz will also be mapped with /xyz.abc or /xyz.abc.test etc. Note that ".*" does not mean that it will also match xyzabc (there has to be a dot after xyz).
Example
A controller
@Controller
public class MyController {
@ResponseBody
@RequestMapping("/employee")
public String employeeHandler(HttpServletRequest request) {
String uri = request.getRequestURI();
return "from employeeHandler, uri: " + uri;
}
}
Java Config:
First we are going to see the default behavior when setUseRegisteredSuffixPatternMatch is set to true.
@Configuration
@ComponentScan
@EnableWebMvc
public class AppConfig{
}
To try examples, run embedded tomcat (configured in pom.xml of example project below):
mvn tomcat7:run-war
Output
As seen in above outputs, all paths are mapped to our single handler method employeeHandler().
Setting UseSuffixPatternMatch=false
Let's configure RequestMappingHandlerMapping with the desired setting:
@Configuration
@ComponentScan
@EnableWebMvc
public class AppConfig {
@Bean
public HandlerMapping requestMappingHandlerMapping() {
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
mapping.setUseSuffixPatternMatch(false);
return mapping;
}
}
As seen above suffix matching doesn't work anymore.
Note
In above example, we can also configure the desired flag by implementing WebMvcConfigurer (which is the preferred way as it does not override the defaults):
@Configuration
@ComponentScan
@EnableWebMvc
public class AppConfig implements WebMvcConfigurer{
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.setUseSuffixPatternMatch(false);
}
}
Example ProjectDependencies and Technologies Used: - spring-webmvc 5.0.4.RELEASE: Spring Web MVC.
- javax.servlet-api 3.0.1 Java Servlet API
- JDK 1.8
- Maven 3.3.9
|