spring-data-commons icon indicating copy to clipboard operation
spring-data-commons copied to clipboard

Pagination / Sort support in http interface client

Open hesselapplications opened this issue 1 year ago • 4 comments

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

  • Page as a response type
  • Pageable / Sort as request parameters
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

hesselapplications avatar Feb 16 '24 16:02 hesselapplications

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.

pukkaone avatar Jun 30 '24 20:06 pukkaone

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.

denniseffing avatar Aug 06 '24 08:08 denniseffing

Hey @mp911de,

I'm facing the same issue described in this ticket. Do you plan to implement this feature? Or it is other project's scope e.g.: spring boot autoconfiguration?

Thanks,

lmagyar89 avatar Aug 13 '25 11:08 lmagyar89

For the time being, it's in our backlog as we hadn't had any chance to come up with a design with our limited bandwidth.

mp911de avatar Aug 13 '25 14:08 mp911de