Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send file path as @PathVariable in Spring MVC

There is a task to pass file path as @PathVariable in Spring MVC to REST Service with GET request.

We can easily do it with POST sending String of file path in JSON.

How we can do with GET request and @Controller like this?

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

Request:

GET /file/getFile/"/Users/user/someSourceFolder/8.jpeg"
Content-Type: application/json
like image 547
J-Alex Avatar asked Jul 19 '16 06:07

J-Alex


2 Answers

You should define your controller like this:

@RequestMapping(value = "/getFile/{path:.+}", method = RequestMethod.GET)
public File getFile(@PathVariable String path) {
    // do something
}
like image 55
Blank Avatar answered Oct 11 '22 09:10

Blank


Ok. you use to get pattern. sending get pattern url.

Use @RequestParam.

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

and if you use @PathVariable.

@RequestMapping(value = "/getFile/{path}", method = RequestMethod.POST)
public File getFile(@PathVariable String path) {
    // do something
}
like image 30
0gam Avatar answered Oct 11 '22 10:10

0gam