9

I have a controller with request mapping as @RequestMapping("/**") What does it mean?

If I want to exclude certain url pattern from the above mapping how would I do that?

Could someone please shed some light on it?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
Chaitanya MSV
  • 6,706
  • 12
  • 40
  • 46

2 Answers2

16

I was able to achieve "url exclusion" or "not matching url" through the use of the Regex "negative lookahead" construct.

I want my handler to handel everything other than static resources, i.e. CSS/Images/JS, and error pages.

To prevent that handeling of error pages i.e. resourceNotFound you will need to

  1. Edit the web.xml /web-app/error-page to prefix the error url with /error/
  2. Edit the WEB-INF/spring/webmvc-config.xml /beans/mvc:view-controller/@path handel your new mappings
  3. Edit the WEB-INF/spring/webmvc-config.xml /beans/bean[@class=**SimpleMappingExceptionResolver] to map all exceptions to error/...

In your controller use the below

@Controller
@RequestMapping(value = { "/" })
public class CmsFrontendController {

  @RequestMapping(value = { "/" }, headers = "Accept=text/html")
  public String index(Model ui) {
      return addMenu(ui, "/");
  }

  @RequestMapping(value = { "{path:(?!resources|error).*$}", "{path:(?!resources|error).*$}/**" }, headers = "Accept=text/html")
  public String index(Model ui, @PathVariable(value = "path")String path) {
      try {
          path = (String) request.getAttribute(
                  HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE);
          return addMenu(ui, path);
      } catch (Exception e) {
          log.error("Failed to render the page. {}", StackTraceUtil.getStackTrace(e));
          return "error/general";
      }
  }
}
concept
  • 761
  • 2
  • 10
  • 16
  • 1
    There MUST be a more maintainable solution than this...? Man. – roufamatic May 29 '13 at 23:27
  • Thanks! This worked great! Been searching for a while for a way to perform excludes – Johan Frick May 15 '15 at 06:38
  • My only problem is that 404s don't work when using thymeleaf. I get a org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing. – DD. Jan 02 '17 at 09:21
8

Your URL will intercept all requests matching '/**'. Depending on where you are defining this I am not sure why you would want to do this. On class level this should define the base path while on method level it should be refined to the specific function.

If you would like to exclude a pattern you could define another controller that is ordered at a higher priority to the controller specifying '/**'

Here are 2 good references from spring source:

  1. http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html

  2. http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html

Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
Kevin
  • 156
  • 1
  • 2
  • 2
    What is the difference between /** and /* . I would think /* will also cover /** Am I correct? – Chaitanya MSV Jan 31 '12 at 02:06
  • @ChaitanyaMSV /* would cover only first part of the url suppose localhost:8080/hello this will only be covered if you give localhost:8080/hello/user you will see a error page. on the other hand /** covers all the parts of the url – Srikanth Josyula Nov 03 '21 at 19:14