Search code examples
javaspringspring-mvcrequest-mapping

Spring-MVC PathVariable matching regular expression not starting with word


I want to match all requests with a PathVariable which are not start with 'api'. I test following RequestMapping. spring could match request but could not get value for PathVariable. How I solve this?

@RequestMapping(value = "/{name:(?!api).+}", method = RequestMethod.GET)
public void getNotApi(@PathVariable String name, HttpServletResponse response) {
    ...
}

I get following message for some request like localhost:8080/resource.

Error 500 Missing URI template variable 'name' for method parameter of type String


Solution

  • @RequestMapping(value = "/{name:^(?!api).+}", method = RequestMethod.GET)