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

心不动则不痛 提交于 2020-08-05 09:38:12

问题


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


回答1:


The exception is related "too many Body parameters", that means you have to send only one object in RequestBody, in this particular case I believe it is MeasureDto "example". The page you have to pass in a different way, generally I have a "page" integer attribute in the class (in your case, MeasureDTO) or I send the page number as @RequestParam, like it:

@PostMapping("/search")
public Page<MeasureDto> searchMeasures(@RequestBody MeasureDto example, @RequestParam(required = true) Integer page) {
 
 // PageRequest.Of(numberPage, quantityRegisters) will send a Pageable, in this case bellow, number of page + number of records
    measureService.getMeasurePageable(example, PageRequest.of(page, 10)); 
 }

Despite of that, consider using GetMapping with queryParams if you are searching things and not saving it.




回答2:


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);


来源:https://stackoverflow.com/questions/63115610/how-to-pass-pageable-to-feign-client-in-post-request-with-additional-requestbod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!