2017-06-22 138 views
2

我想将所有请求与不以'api'开头的PathVariable匹配。我测试下面的RequestMapping。 spring可以匹配请求,但无法为PathVariable获取价值。我如何解决这个问题?Spring-MVC PathVariable匹配不以word开头的正则表达式

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

我获得以下像localhost:8080/resource一些请求消息。

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

回答

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

如果你正在构建的Java前8的版本,这可能是因为缺少名称。尝试@PathVariable("name") String name。保持@PathVariable的值与您的映射参数名称相同。