1

I have the following problem. Please help. I get a JSON array in URL like this:

[{
    "property": "bt",
    "value": "ATM"
}, {
    "property": "status",
    "value": null
}, {
    "property": "lf",
    "value": ""
}, {
    "property": "accessibility",
    "value": null
}]

Here is an URL example (encoded and decoded)

/locations/search?_dc=1479465653885&page=1&start=0&limit=25&filter=%5B%7B%22property%22%3A%22bt%22%2C%22value%22%3A%22ATM%22%7D%2C%7B%22property%22%3A%22status%22%2C%22value%22%3Anull%7D%2C%7B%22property%22%3A%22lf%22%2C%22value%22%3A%22%22%7D%2C%7B%22property%22%3A%22accessibility%22%2C%22value%22%3Anull%7D%5D

/locations/search?_dc=1479379135922&page=1&start=0&limit=25&filter=[{"property":"bt","value":"ATM"},{"property":"status","value":null},{"property":"lf","value":""},{"property":"accessibility","value":""}]

I want to accept that in my controller but I get: 500 Internal server error

//Controller
@GetMapping("/search")
public Page<LocationDto> getFilteredList(@RequestParam Filter[] filter, Pageable pageable) {
    //some code
}

//stack trace
java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [hr.sberbank.il24.locations.controllers.LocationController$Filter]: no matching editors or conversion strategy found

I've made this work by changing Filter[] to String and using Jackson directly, but I want to know why Spring won't do that automatically and is that possible at all(GET+@RequestParam+JSON array in URL)? Can I make some custom converter that'll be automatically used by Spring?

//Controller
@GetMapping("/search")
public Page<LocationDto> getFilteredList(@RequestParam String filterStr, Pageable pageable) {
    //some code
    Filter[] filters = new ObjectMapper().readValue(filterStr), Filter[].class);
    //somed code
}
Ivan
  • 71
  • 10
  • This is clearer now. I don't think this is a duplicate of referenced link, although they are similar, especially with the title. The main question here was WHY Spring doesn't do automatic conversion. This is because Spring doesn't expect JSON in URL and we should define a converter. When I change to POST method and @RequestBody Spring does automatic conversion (no converter needed) because it naturally expects JSON in body of the POST request. – Ivan Nov 21 '16 at 13:51

0 Answers0