Spring MVC - URI suffix pattern matching behavior, Using setUseRegisteredSuffixPatternMatch() method of RequestMappingHandlerMapping

[Last Updated: Mar 7, 2018]

In this example, we will understand the effect of using method setUseRegisteredSuffixPatternMatch() of RequestMappingHandlerMapping. This flag specifies whether suffix pattern matching (last tutorial) should work only against path extensions explicitly registered with the ContentNegotiationManager (check out content negotiation tutorial). By default this flag is set to "false".


A controller

public class MyController {

  public String employeeHandler(HttpServletRequest request) {
      String uri = request.getRequestURI();
          return "<doc>test response at "+uri+"</doc>";
      return "plain test response at "+uri;

Java Config:

First we are going to see the default behavior when setUseRegisteredSuffixPatternMatch is set to false.

 public class AppConfig implements WebMvcConfigurer {

To try examples, run embedded tomcat (configured in pom.xml of example project below):

mvn tomcat7:run-war


As seen in above outputs, all paths are mapped to our single handler method employeeHandler() and the one with xml extension returns response in xml (even if we return the plain string). The xml response is because of ServletPathExtensionContentNegotiationStrategy (tutorial here) which is enabled by default.

Let's say we want to only get plain response with uri '/employee' or with uri '/employee.xml' and disable the rest. For that we have to set setUseRegisteredSuffixPatternMatch to true.

Setting setUseRegisteredSuffixPatternMatch=true

Let's configure RequestMappingHandlerMapping with the desired setting:

public class AppConfig implements WebMvcConfigurer {

  public void configurePathMatch(PathMatchConfigurer configurer) {

Example Project

Dependencies 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

setUseRegisteredSuffixPatternMatch() Example Select All Download
  • spring-use-registered-suffix-pattern-match
    • src
      • main
        • java
          • com
            • logicbig
              • example

    See Also