如何匹配具有@pathVariable且包含"/"的Spring @RequestMapping? [英] How to match a Spring @RequestMapping having a @pathVariable containing "/"?
问题描述
我正在执行来自客户端的以下请求:
I am doing the following request from the client:
/search/hello%2Fthere/
已对URL编码搜索词"hello/there".
where the search term "hello/there" has been URLencoded.
在服务器上,我尝试使用以下请求映射来匹配此URL:
On the server I am trying to match this URL using the following request mapping:
@RequestMapping("/search/{searchTerm}/")
public Map searchWithSearchTerm(@PathVariable String searchTerm) {
// more code here
}
但是我在服务器上收到错误404,因为我没有与URL匹配的内容.我注意到,URL在Spring获取之前就已解码.因此正在尝试匹配/search/hello/那里没有任何匹配项.
But I am getting error 404 on the server, due I don't have any match for the URL. I noticed that the URL is decoded before Spring gets it. Therefore is trying to match /search/hello/there which does not have any match.
我在这里找到了与此问题有关的Jira: http://jira.springframework.org/Browse/SPR-6780 .但是我仍然不知道如何解决我的问题.
I found a Jira related to this problem here: http://jira.springframework.org/browse/SPR-6780 .But I still don't know how to solve my problem.
有什么想法吗?
谢谢
推荐答案
没有很好的方法(不处理HttpServletResponse
).您可以执行以下操作:
There are no good ways to do it (without dealing with HttpServletResponse
). You can do something like this:
@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());
...
}
这篇关于如何匹配具有@pathVariable且包含"/"的Spring @RequestMapping?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!