Issue
I'm working on a webapp, one function of which was to list all the files under given path. I tried to map several segments of URL to one PathVariable like this :
@RequestMapping("/list/{path}")
public String listFilesUnderPath(@PathVariable String path, Model model) {
//.... add the file list to the model
return "list"; //the model name
}
It didn't work. When the request url was like /list/folder_a/folder_aa
, RequestMappingHandlerMapping
complained : "Did not find handler method for ..."
Since the given path could contains any number of segments, it's not practical to write a method for every possible situation.
Solution
In REST each URL is a separate resource, so I don't think you can have a generic solution. I can think of two options
- One option is to change the mapping to
@RequestMapping("/list/**")
(path
parameter no longer needed) and extract the whole path from request - Second option is to create several methods, with mappings like
@RequestMapping("/list/{level1}")
,@RequestMapping("/list/{level1}/{level2}")
,@RequestMapping("/list/{level1}/{level2}/{level3}")
... concatenate the path in method bodies and call one method that does the job. This, of course, has a downside that you can only support a limited folder depth (you can make a dozen methods with these mappings if it's not too ugly for you)
Answered By - Predrag Maric
Answer Checked By - Candace Johnson (JavaFixing Volunteer)