Search code examples
javaspringspring-bootfeignopenfeign

How to pass Pageable to Feign Client in POST Request with additional @RequestBody


I tried to create a feign client for my REST service controller in Spring.

@PostMapping("/search")
public Page<MeasureDto> searchMeasures(@RequestBody MeasureDto example, Pageable pageable) {
    ...
}

The client looks like this:

@PostMapping("/search")
public Page<MeasureDto> searchMeasures(@RequestHeader("apiKey") String apiKey, @RequestBody MeasureDto example, Pageable pageable);

Following exception is thrown when running a test:

Caused by: java.lang.IllegalStateException: Method has too many Body parameters: public abstract org.springframework.data.domain.Page com.foo.bar.jobservice.client.MeasureServiceClient.searchMeasures(java.lang.String,com.example.foo.jobservice.client.dto.MeasureDto,org.springframework.data.domain.Pageable)

What I already know/tried:

There is a closed issue on github: https://github.com/spring-cloud/spring-cloud-netflix/issues/556

The issue with the commit that should have resolved the problem:

https://github.com/spring-cloud/spring-cloud-openfeign/issues/26

The commit:

https://github.com/spring-cloud/spring-cloud-openfeign/commit/6e0e63644ba34193f03c2cd74391cac73b9bfdb4

What i configured:

import feign.codec.Encoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.support.PageJacksonModule;
import org.springframework.cloud.openfeign.support.PageableSpringEncoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@EnableFeignClients
@Configuration
public class FeignConfig {

    @Bean
    public PageJacksonModule pageJacksonModule() {
        return new PageJacksonModule();
    }

    @Autowired
    private ObjectFactory<HttpMessageConverters> messageConverters;

    @Bean
    public Encoder feignEncoder() {
        return new PageableSpringEncoder(new SpringEncoder(messageConverters));
    }
}

Still not working.

What I am using:

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>

<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.3.RELEASE</version>

What am I doing wrong?


Update:

I created a reproducable example:

https://github.com/manuelwaltschek/mre.git

Start up the client service or call spring/spring-cloud-openfeign/375-pageable-not-working/parent/client/src/test/java/com/example/client/HelloServiceClientTest.java

Open issues on github: https://github.com/spring-cloud/spring-cloud-openfeign/issues/375 https://github.com/spring-cloud/spring-cloud-openfeign/issues/385

Edit: Basically I want to know how to pass the pageable to the feign client. Maybe encode it in url params?

Related question: how to pass Spring Pageable to FeignClient


Solution

  • I'm now sending the pageable in query url params with org.springframework.cloud.openfeign.SpringQueryMap annotation on my Pageable Parameter in the Client interface

    @PostMapping("/search")
    public Page<HelloDto> searchHellos(@RequestHeader("apiKey") String apiKey, 
                                       @RequestBody HelloDto example,
                                       @SpringQueryMap Pageable pageable);