Search code examples
springspring-mvcrestful-urlspring-restcontrollerrequest-mapping

Spring REST request mapping


I want to configure Spring to redirect all the requests to a specific Controller irrespective of the URL (length and parameters). How should I give the URL pattern / regular expression in the RequestMapping annotation. I tried using the below code, but it is not working. Any help in this regard is deeply appreciated.

    @Controller
    @RequestMapping("/*")
    public class ServiceController {
        @RequestMapping( method = RequestMethod.GET, value = "*" )
        public void getProjectList(HttpServletRequest httpServletRequest,Model model){

        }
    }

Solution

  • You need @RequestMapping(method = RequestMethod.GET, value = "/**").

    From http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-patterns:

    In addition to URI templates, the @RequestMapping annotation also supports Ant-style path patterns (for example, /myPath/*.do).

    From http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/util/AntPathMatcher.html:

    The mapping matches URLs using the following rules:

    • ? matches one character
    • * matches zero or more characters
    • ** matches zero or more directories in a path