我试图在 Spring 中为我的 REST 服务控制器创建一个 feign 客户端。
@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);
运行测试时抛出以下异常:
引起: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)
我已经知道/尝试过的:
github上有一个已关闭的issue:https ://github.com/spring-cloud/spring-cloud-netflix/issues/556
应该解决问题的提交的问题:
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>
我究竟做错了什么?
更新:
我创建了一个可重现的示例:
https://github.com/manuelwaltschek/mre.git
启动客户端服务或调用 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/issues/375 https://github.com/spring-cloud/spring-cloud-openfeign/issues/385
编辑:基本上我想知道如何将 pageable 传递给 feign 客户端。也许在 url 参数中对其进行编码?