How to map several segments of URL to one PathVariable in spring-mvc?

后端 未结 1 540
谎友^
谎友^ 2021-01-25 12:29

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 :



        
相关标签:
1条回答
  • 2021-01-25 13:11

    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)
    0 讨论(0)
提交回复
热议问题