【问题标题】:How to enable greedy @PathVariable for a particular @RequestMapping? [duplicate]如何为特定的@RequestMapping 启用贪婪的@PathVariable? [复制]
【发布时间】:2017-06-14 16:59:45
【问题描述】:

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

@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

【问题讨论】:

  • 您只能使用@RequestParam 传递包含/ 的URL,但不能使用@PathVariable

标签: spring spring-restcontroller


【解决方案1】:

如果您在路径变量中发送 dir/subdir,则需要转义路径变量中的“/”字符。 您需要在请求处理程序级别encode url 的路径变量部分和decode

【讨论】:

  • 不,这不是编码问题。 url解码是由spring透明地完成的,显然是在url被解析以搜索请求映射之前
猜你喜欢
  • 2013-02-04
  • 2021-02-09
  • 1970-01-01
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多