如何为特定的@RequestMapping启用贪婪的@PathVariable?

时间:2017-01-29 11:57:23

标签: spring spring-restcontroller

我尝试通过休息服务启用目录浏览。它的定义如下:

@RequestMapping(path="ls/{path:.+}", method = RequestMethod.GET)
public List<String> getDirectoryListing(@PathVariable("path") String path) throws IOException {
  // ...
}

但是,如果我使用类似目录的定义,PathVariable {path:.+}会引入问题。例如,

$ curl http://localhost:8080/fileRepo/ls/dir/subdir
                                         \___ ___/
                                             V 
                                          {path:.+} 

将导致

{
 "timestamp":1485690489272,
 "status":404,"error":"Not Found",
 "message":"No message available",
 "path":"/dataset/ls/processed/dir1/subdir"
 }

并在控制台上显示调试消息

s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /files/ls/dir1/subdir
s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/files/ls/dir1/subdir]
o.s.w.s.handler.SimpleUrlHandlerMapping  : Matching patterns for request [/files/ls/dir1/subdir] are [/**]
o.s.w.s.handler.SimpleUrlHandlerMapping  : URI Template variables for request [/files/ls/dir1/subdir] are {}

因此。如何告诉RequestMappingHandlerMapping,ls/{path:.+}应该全部解析为getDirectoryListing

1 个答案:

答案 0 :(得分:-1)

如果要在路径变量中发送dir / subdir,则需要转义&#34; /&#34;路径变量中的字符。 您需要在请求处理程序级别编码您的网址的路径变量部分,并解码