views:

395

answers:

2

Hi

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.

Any ideas?

Thanks

+2  A: 

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());

    ...
}
axtavt
A: 

I have been thinking in two different approaches:

  • use a "special" character to replace the "/" in the client and later on, do the opposite in the server (not very nice though).
  • do not use REST URLs in the controller.

My conclusion is that search terms should not be sent in REST-style. I will use query parameter in this case.

yeforriak