How to match a Spring @RequestMapping having a @pathVariable containing "/"?
I am doing the following request from the client:
/search/hello%2Fthere/
where the search term "hello/there" has been URLencoded.
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
}
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.
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.开发者_如何学C
Any ideas?
Thanks
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());
...
}
@Configuration
public class AdditionalWebConfig extends WebMvcConfigurationSupport {
.......
...........
...........
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer
configurer) {
configurer.favorPathExtension(false);
}
.......
.......
}
and add regex like this in @PathVariable("user/{username:.+}")
精彩评论