0

I have the following REST- method:

@RequestMapping(value = "/search/accounts/{searchterm}", method = RequestMethod.GET, produces = "application/json")
public @ResponseBody Set<Contact> findAccounts(@PathVariable("searchterm")
final String searchTerm) throws BusinessException {

and it works fine until searchTerm includes a slash, e.g. 3/2015, then the url looks like this:

/search/accounts/3/2015 

and the method can not be found. My question now would be if there is a possibility to solve this. In frontend I use Angular 1.4

quma
  • 5,233
  • 26
  • 80
  • 146
  • Seems like using a @RequestParam would be more appropriate. so you have something like: ...findAccounts(@RequestParam String date) and your URL could /search/accounts?date=3/2015 – RuntimeBlairror Oct 13 '16 at 20:11
  • Possible duplicate of [Has problems after @PathVariable parameter value containing the '/' character,with Spring MVC](http://stackoverflow.com/questions/15385312/has-problems-after-pathvariable-parameter-value-containing-the-character-wi) – Chris Thompson Oct 13 '16 at 23:22

1 Answers1

0

You could URL-encode it on the client side and then decode it on the server, but it is a non-standard approach in Spring and will get you in trouble in a long run. Try to search with a term that contains a dot (.) for example.

I'd recommend changing your controller to accept the search term as query parameter (properly encoded of course):

/search/accounts?searchterm=3%2F2015

The controller method should then be:

@RequestMapping(
    value = "/search/accounts", 
    method = RequestMethod.GET, 
    produces = "application/json")
public @ResponseBody Set<Contact> findAccounts(
    @RequestParam("searchterm") final String searchTerm) throws BusinessException {
    (...)
}
jannis
  • 4,843
  • 1
  • 23
  • 53