spring-projects / spring-data-commons

Spring Data Commons. Interfaces and code shared between the various datastore specific implementations.
https://spring.io/projects/spring-data
Apache License 2.0
777 stars 675 forks source link

Pagination / Sort support in http interface client #3046

Open hesselapplications opened 9 months ago

hesselapplications commented 9 months ago

It would be great if http interface clients had support for:

interface FooClient {
  @GetExchange("/foos")
  public fun getFoos(
    pageRequest: Pageable,
  ): Page<Foo>
}

This would bring feature parity for what is supported in controllers today. I believe spring openfeign clients support this as well

Original: https://github.com/spring-projects/spring-framework/issues/32286

pukkaone commented 4 months ago

We have Pageable parameters on Spring Cloud OpenFeign client methods. The lack of Pageable support blocks us from switching to HTTP interface clients. I'm experimenting with this work around. Define a custom argument resolver:

public class PageableArgumentResolver implements HttpServiceArgumentResolver {

    @Override
    public boolean resolve(Object argument, MethodParameter parameter, HttpRequestValues.Builder requestValues) {
        if (!parameter.getParameterType().equals(Pageable.class)) {
            return false;
        }

        if (argument != null) {
            var pageable = (Pageable) argument;

            if (pageable.isPaged()) {
                requestValues.addRequestParameter("page", String.valueOf(pageable.getPageNumber()));
                requestValues.addRequestParameter("size", String.valueOf(pageable.getPageSize()));
            }

            Sort sort = pageable.getSort();
            if (sort.isSorted()) {
                for (Sort.Order order : sort) {
                    requestValues.addRequestParameter("sort", order.getProperty() + "," + order.getDirection().name());
                }
            }
        }

        return true;
    }
}

Invoke the HttpServiceProxyFactory.Builder customArgumentResolver method to register the custom argument resolver.

denniseffing commented 3 months ago

We started implementing an HTTP client using an HTTP interface client as well and just stumbled upon this issue after researching how to implement pagination.

I guess the answer is: We can't. Unfortunate 😄

So +1 for this issue.