Situation
We create a simple API to get the bars belonging to this Foo using pagination, for example:
GET /foo/1/bar?pageSize=50&pageOffset=1
The backend uses Spring support for paganizing JPA data, for example:
Page<Bar> barsPage = repo.findBarsForFoo(fooId, new PageRequest(pageOffset, pageSize));
Problem
We need to filter out certain elements from barPage - easily, for example:
List<Bar> filtered = barsFilter.removeSome(barsPage.getContent())
Filtering component delegates to other services (let them refuse them):
class BarsFilter {
@Autowired Rejector1 rejector1;
@Autowired Rejector2 rejector2;
public List<Bar> filterSome(List<Bar> bars) {
return bars.stream()
.filter(bar -> rejector1.matches(bar))
.filter(bar -> rejector2.matches(bar))
.collect(toList());
}
}
The problem is that the API client will receive fewer elements than they requested.
Gotchas
The filtering logic is complex and depends on other components. It cannot be written as a single database query.
? ? ? Spring Data JPA ?