如何匹配包含“/”的@pathVariable的Spring @RequestMapping?

我正在做客户的以下请求:

/search/hello%2Fthere/

其中搜索词“hello / there”已经过URL编码。

在服务器上,我尝试使用以下请求映射匹配此URL:

 @RequestMapping("/search/{searchTerm}/") public Map searchWithSearchTerm(@PathVariable String searchTerm) { // more code here } 

但我在服务器上收到错误404,因为我没有任何匹配的URL。 我注意到在Spring获取之前解码了URL。 因此试图匹配/ search / hello /那里没有任何匹配。

我在这里发现了一个与此问题相关的Jira: http : //jira.springframework.org/browse/SPR-6780 。但我仍然不知道如何解决我的问题。

有任何想法吗?

谢谢

没有好的方法(没有处理HttpServletResponse )。 你可以这样做:

 @RequestMapping("/search/**") public Map searchWithSearchTerm(HttpServletRequest request) { // Don't repeat a pattern String pattern = (String) request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE); String searchTerm = new AntPathMatcher().extractPathWithinPattern(pattern, request.getServletPath()); ... }