Close

Spring MVC - HandlerMapping setAlwaysUseFullPath() Example

[Last Updated: Jun 29, 2017]

In this example, we will understand the effect of using method setAlwaysUseFullPath() of AbstractHandlerMapping. Typically, all HandlerMapping classes extends AbstractHandlerMapping, so this method is applicable for all of them.

If alwaysUseFullPath property is set to true, then the matching algorithm uses complete URI path for matching the handler. This path includes the path specified by the parent servlet mapping but still does not include the context and preceding part.

Example

In this example, we are going to use default RequestMappingHandlerMapping strategy, which uses URI specified by @RequestMapping.

Let's specify a base path for DispatcherServlet:

public class AppInitializer extends
        AbstractAnnotationConfigDispatcherServletInitializer {

  @Override
  protected Class<?>[] getRootConfigClasses () {
      return new Class<?>[]{AppConfig.class};
  }

  @Override
  protected Class<?>[] getServletConfigClasses () {
      return null;
  }

  @Override
  protected String[] getServletMappings () {
      return new String[]{"/app/*"};
  }
}

Now we will analyze the behavior with alwaysUseFullPath=false (default) and then alwaysUseFullPath=true .

Using alwaysUseFullPath=false (default)

The configuration class:

@EnableWebMvc
@Configuration
@ComponentScan
public class AppConfig {
}

The Controller:

@Controller
public class ExampleController {
  
  @RequestMapping("/example")
  @ResponseBody
  public String handle () {
      return "response from ExampleController";
  }
}

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

mvn tomcat7:run-war

Output

It works as usual.

Using alwaysUseFullPath=true

Let's configure RequestMappingHandlerMapping with the desired setting:

@Configuration
@ComponentScan
public class AppConfig extends WebMvcConfigurationSupport {
  
  @Override
  @Bean
  public RequestMappingHandlerMapping requestMappingHandlerMapping () {
      RequestMappingHandlerMapping m = super.requestMappingHandlerMapping();
      m.setAlwaysUseFullPath(true);
      return m;
  }
}

With the same AppInitializer and the controller, let's access the URL after restarting the server:

Why we can't access the page anymore? That's because, now we are required to always use the full path. In our controller @RequestMapping("/example") has to be full path.

Let's modify our controller:

@Controller
public class ExampleController {
  
  @RequestMapping("/app/example")
  @ResponseBody
  public String handle () {
      return "response from ExampleController";
  }
}

Restart the server without any other changes:

Example Project

Dependencies and Technologies Used:

  • spring-webmvc 4.3.9.RELEASE: Spring Web MVC.
  • javax.servlet-api 3.0.1 Java Servlet API
  • JDK 1.8
  • Maven 3.3.9

Alawys Use Full Path Example Select All Download
  • always-use-full-path
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • AppConfig.java

    See Also