user9735824 :
I have a rest service that will return List
of objects
public class MyObject {
private String name;
private String state;
}
Now, I need to filter object from list based on fields provided on rest call:
http://localhost:8080/myuri?state=NY
Now, I need to develop custom filter and I did only find property filter not something like I want. Is there a way to achieve this.
Michał Ziober :
You do not need to use Jackson
to do this. Just filter it using Stream API
. If data, are loaded from DB
, filter it using SQL
's WHERE
clause.
Example:
@GetMapping(value = "/states", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<MyObject> loadStates(@RequestParam(name = "state", defaultValue = "NY", required = false) String[] states) {
return service.loadAndFilterByState(states);
}
If you have a cached list, you can filter it like below:
@GetMapping(value = "/states", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<MyObject> loadStates(@RequestParam(name = "state", defaultValue = "NY", required = false) String[] states) {
Arrays.sort(states);
return getStates()
.stream()
.filter(s -> Arrays.binarySearch(states, s.getState()) > -1)
.collect(Collectors.toList());
}
See also:
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=328685&siteId=1