在Spring MVC中将文件路径发送为@PathVariable


问题内容

@PathVariableSpring MVC 一样,有一项任务可以通过GET请求将文件路径传递给REST服务。

我们可以POST通过在JSON中发送文件路径的String 轻松实现。

我们该如何处理这样的GET请求@Controller

@RequestMapping(value = "/getFile", method = RequestMethod.GET)
public File getFile(@PathVariable String path) {
    // do something
}

请求:

GET /file/getFile/"/Users/user/someSourceFolder/8.jpeg"
Content-Type: application/json

问题答案:

好。您用来获取图案。发送获取模式网址。

使用@RequestParam。

@RequestMapping(value = "/getFile", method = RequestMethod.GET)
public File getFile(@RequestParam("path") String path) {
    // do something
}

以及是否使用@PathVariable。

@RequestMapping(value = "/getFile/{path}", method = RequestMethod.POST)
public File getFile(@PathVariable String path) {
    // do something
}