3

我试图在 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

提交:

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

我配置了什么:

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 参数中对其进行编码?

相关问题:如何将 Spring Pageable 传递给 FeignClient

4

2 回答 2

5

我现在在客户端界面中的 Pageable 参数上使用 org.springframework.cloud.openfeign.SpringQueryMap 注释在查询 url 参数中发送可分页

@PostMapping("/search")
public Page<HelloDto> searchHellos(@RequestHeader("apiKey") String apiKey, 
                                   @RequestBody HelloDto example,
                                   @SpringQueryMap Pageable pageable);
于 2020-07-29T12:51:30.537 回答
2

异常与“太多的 Body 参数”有关,这意味着您必须在 RequestBody 中只发送一个对象,在这种特殊情况下,我相信它是 MeasureDto“示例”。您必须以不同的方式传递的页面,通常我在类中有一个“页面”整数属性(在您的情况下为 MeasureDTO),或者我将页码作为@RequestParam 发送,如下所示:

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

尽管如此,如果您正在搜索而不是保存它,请考虑将 GetMapping 与 queryParams 一起使用。

于 2020-07-27T13:14:14.337 回答