如何在Spring MVC中匹配任何路径级别的扩展?

时间:2014-07-13 08:50:08

标签: java regex spring spring-mvc

我需要一个只匹配任何路径级别图像的请求映射器。

例如,它必须匹配;

http://localhost:8080/filename.jpg
http://localhost:8080/figs/filename.jpg

我尝试了正则表达式和非正则表达式;

@RequestMapping(value = "{reg:(?:\\w|\\W)+\\.(?:jpg|bmp|gif|jpeg|png|webp)$}", method = RequestMethod.GET)

但是,它与http://localhost:8080/filename.jpg匹配,但与http://localhost:8080/figs/filename.jpghttp://localhost:8080/anypathlevelNtimes/ figs/filename.jpg不匹配

它如何与所有路径级别匹配,文件扩展名只是图像。

编辑:现在没关系

@RequestMapping(value = {"/**/{extension:(?:\\w|\\W)+\\.(?:jpg|bmp|gif|jpeg|png|webp)$}"}, method = RequestMethod.GET)

2 个答案:

答案 0 :(得分:1)

OP提出的解决方案:

@RequestMapping( //
       value  = {"/**/{extension:(?:\\w|\\W)+\\.(?:jpg|bmp|gif|jpeg|png|webp)$}"}, //
       method = RequestMethod.GET //
)

答案 1 :(得分:-1)

@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
public void handle(@PathVariable String version, @PathVariable String extension) {
    // ...
}

reference https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html

https://docs.spring.io/spring/docs/3.0.x/javadoc-api/org/springframework/util/AntPathMatcher.html