Springboot Requestmapper正则表达式

问题描述 投票:0回答:1

在我的Springboot-Controller中,我的Requestmapping方法应该支持灵活的子目录名称。为此,我喜欢使用真正的正则表达式。我已经在SpringBoot和RequestMapping之外测试了我的正则表达式及其工作正常,但是在RequestMapping之上,它不起作用。

如果有任何http请求进入

[http://dir1http://dir2

我的方法getFile应该被调用,但是没有。

@RequestMapping(value = "{reg:\\/(dir1|dir2)+}", method = RequestMethod.GET)    
public void getFile(HttpServletResponse response, HttpServletRequest requ) throws IOException {

}

我的问题是如何完成此任务....

regex spring-boot subdirectory request-mapping
1个回答
0
投票

基于正则表达式的@RequestHandler可以通过(for more)实现

    @RequestMapping(value = "{reg:regexPattern}", method = RequestMethod.GET)
    public void getFile(HttpServletRequest request, HttpServletResponse response, 
            @PathVariable String reg) throws IOException {
                 ...
             }

但是在您的情况下,正则表达式模式是包含Slash(“ /”)的目录值,这使得请求处理程序很难找到确切的映射。您可以使用@PathVariable

代替@RequestParam

    @RequestMapping(value = "\", method = RequestMethod.GET)
    public void getFile(HttpServletRequest request, HttpServletResponse response, 
          @RequestParam("dir") String dir) throws IOException {
        ...
    }

© www.soinside.com 2019 - 2024. All rights reserved.