【问题标题】:how to pass Spring Pageable to FeignClient如何将 Spring Pageable 传递给 FeignClient
【发布时间】:2020-11-18 22:59:18
【问题描述】:

正如我在本期中所写:https://github.com/spring-cloud/spring-cloud-openfeign/issues/375 在这个 SO 问题中: Spring data Pageable does not work with feign client

在包含 @requestbody 元素的 POST 请求中发送 Pageable 对象时遇到一些问题。

首先我尝试发送嵌入在我的 RequestBody 中的 Pageable,因为它是在使用 springdoc-openapi-webmvc-core 时作为示例生成的。

例子:

@PostMapping("/search") public Page<HelloDto> searchHellos(@RequestBody HelloDto example, Pageable pageable)

{ "example": { "message": "string" }, "pageable": { "pageNumber": 0, "pageSize": 50, "offset": 10, "sort": { "sorted": false, }, "paged": true, } }

不,我知道,我也可以通过 Query 参数发送它,它会被解析。将pageable提取到QueryParams中的注解在SpringDoc中称为org.springdoc.api.annotations.ParameterObject。

@PostMapping("/search") public Page<HelloDto> searchHellos(@RequestBody HelloDto example, @ParameterObject Pageable pageable) { return helloCallerService.callHelloServiceClient(example, pageable); }

当我尝试像这个客户端接口一样通过 feign 调用类似的控制器接口时:

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

应用程序无法启动,因为该方法“具有太多的正文参数”。 我试图用 @RequestParam 注释 Pageable 但它不会被识别/解析。

我的问题是:

我们可以使用自定义 AnnotatedParameterProcessor 吗? @ParameterObject 将 ParameterObject 扁平化并将其编码为 url 查询参数?

你会怎么做?还是 AnnotatedParameterProcessor 方法不对?我可能需要扩展器或其他什么东西吗?

【问题讨论】:

  • 你设法让它工作了吗?我已经看到您关于使用 @SpringQueryMap 的回答,但在我的情况下,我需要修改一些可分页的查询参数。例如:sortField: DESC 应该变成 -sortField
  • 对不起,我很幸运它正确解析了我的 Pageable(至少看起来是这样)。我不知道您如何对此进行更多控制。也许可以使用 Expander、Param 注释和 RequestLine 来寻找解决方案,就像在这个例子中一样:stackoverflow.com/questions/43868680/… 我也更喜欢带有配置选项的更通用的解决方案。如果您能解决您的问题,请告诉我。
  • 嗯,也在研究自定义 AnnotatedParameterProcessor,但还没有成功。我也在使用 reactive-feign 实现并在那里添加了一张票:github.com/Playtika/feign-reactive/issues/271
  • 我最终创建了自己的从 HashMap 扩展的对象。然后它可以用作普通的@RequestParam。我添加了一个构造函数,它接受一个 Pageable 并将正确的信息放入地图中

标签: spring-boot spring-mvc spring-cloud-feign springdoc


【解决方案1】:

您的问题比 springdoc-openapi 更与 feign 使用有关。 使用 org.springframework.cloud.openfeign.SpringQueryMap,可以解决您的问题。

【讨论】:

  • 谢谢,我的答案已被删除,但这正是我使用的。
猜你喜欢
  • 2021-11-14
  • 2016-04-03
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
  • 2011-04-15
  • 1970-01-01
  • 2019-01-29
  • 2012-06-06
相关资源
最近更新 更多