user3318489 user3318489 - 1 month ago 11
reST (reStructuredText) Question

AngularJS in frontend String Rest Service in backend - search for string containing slash

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

Answer

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 {
    (...)
}
Comments