Search code examples
javaspringspring-mvcpath-variables

How does Spring MVC @PathVariable receive parameters with multiple '/'?


I was working on a file upload widget for managing images.

I wish that image paths can be received via @PathVariable in Spring MVC, such as http://localhost:8080/show/img/20181106/sample.jpg instead of http://localhost:8080/show?imagePath=/img/20181106/sample.jpg.

But / will be resolved Spring MVC, and it will always return 404 when accessing.

Is there any good way around this?


Solution

  • You can use like below.

    @RequestMapping(value = "/show/{path:.+}", method = RequestMethod.GET)
    public File getImage(@PathVariable String path) {
            // logic goes here
    }
    

    Here .+ is a regexp match, it will not truncate .jpg in your path.