如何使用额外的@RequestBody 在 POST 请求中将 Pageable 传递给 Feign Client [英] How to pass Pageable to Feign Client in POST Request with additional @RequestBody
问题描述
我尝试在 Spring 中为我的 REST 服务控制器创建一个伪装客户端.
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) {
...
}
客户端看起来像这样:
@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)
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:
github 上有一个封闭问题:https://github.com/spring-cloud/spring-cloud-netflix/issues/556
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
提交:
我配置了什么:
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));
}
}
还是不行.
我正在使用什么:
<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>
我做错了什么?
更新:
我创建了一个可重现的示例:
I created a reproducable example:
https://github.com/manuelwaltschek/mre.git
启动客户端服务或调用spring/spring-cloud-openfeign/375-pageable-not-working/parent/client/src/test/java/com/example/client/HelloServiceClientTest.java
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
github 上的未解决问题:https://github.com/spring-cloud/spring-cloud-openfeign/问题/375https://github.com/spring-cloud/spring-cloud-openfeign/issues/385
Open issues on github: https://github.com/spring-cloud/spring-cloud-openfeign/issues/375 https://github.com/spring-cloud/spring-cloud-openfeign/issues/385
基本上我想知道如何将可分页传递给伪装客户端.也许将其编码在 url 参数中?
Basically I want to know how to pass the pageable to the feign client. Maybe encode it in url params?
相关问题:如何将 Spring Pageable 传递给 FeignClient
推荐答案
我现在在客户端界面中的 Pageable 参数上使用 org.springframework.cloud.openfeign.SpringQueryMap 注释发送查询 url 参数中的可分页
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);
这篇关于如何使用额外的@RequestBody 在 POST 请求中将 Pageable 传递给 Feign Client的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!