0

I have a REST controller with a RequestMapping that looks like this:

@RequestMapping(method = RequestMethod.GET)
public List<MyDTO> search(SearchParameters searchParameters) {
        // ...
}

and call it like that: /data/search?name=some%20value&....

searchParameters is populated, but the values are not being urldecoded. So instead of setting searchParameter's attribute name to "some value" it is "some%20value". How can I instruct Spring to urldecode these values?

user1068464
  • 325
  • 3
  • 12
  • 1
    why not use a `@RequestParam("yourparametername")String yourparameter` for each request parameter instead of a DTO, this is proven to be working. – zakaria amine May 27 '17 at 07:58
  • There are about 15 search parameters, it's tedious to write out and hard to read afterwards. – user1068464 May 27 '17 at 08:40
  • See: https://stackoverflow.com/questions/2632175/decoding-uri-query-string-in-java, or check out my answer below for an alternative solution. – zakaria amine May 27 '17 at 09:03

2 Answers2

1

One possible solution is to use a Map and have their names stored statically in a class, like :

@RequestMapping(method = RequestMethod.GET)
public List<MyDTO> search(@RequestParam Map<String,String> parameters) {
        String name = parameters.get(SearchParameters.NAME);
// ...
}

or use the Map to build the Object SearchParameters:

  @RequestMapping(method = RequestMethod.GET)
    public List<MyDTO> search(@RequestParam Map<String,String> parameters) {
            SeachParameters searchParameters = new SearchParameters(parameters);
    // ...
    }
zakaria amine
  • 3,412
  • 2
  • 20
  • 35
0

Okay, the problem was not actually the decoding, but the encoding. It was encoded in a test case, although it wouldn't have needed to be. So the URL looking like /data/search?name=some%20value should really have been /data/search?name=some value with actual spaces.

user1068464
  • 325
  • 3
  • 12