如何将 Spring Pageable 传递给 FeignClient [英] how to pass Spring Pageable to FeignClient
问题描述
正如我在本期中所写:https://github.com/spring-cloud/spring-cloud-openfeign/issues/375在这个问题中:Spring 数据 Pageable 不适用于 feign 客户端
as I wrote in this issue: https://github.com/spring-cloud/spring-cloud-openfeign/issues/375 and in this SO question: Spring data Pageable does not work with feign client
在包含@requestbody 元素的 POST 请求中发送 Pageable 对象时遇到一些问题.
I have some problems sending a Pageable object in a POST request containing a @requestbody element.
首先,我尝试发送嵌入在我的 RequestBody 中的 Pageable,因为它是在使用 springdoc-openapi-webmvc-core 时作为示例生成的.
First I tried sending the Pageable embedded in my RequestBody since it was generated as example when using 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.
No I learned, that I can send it via Query parameter aswell and it will be parsed. The annotation that extracts the pageable into QueryParams is called org.springdoc.api.annotations.ParameterObject in SpringDoc.
@PostMapping("/search") public Page<HelloDto> searchHellos(@RequestBody HelloDto example, @ParameterObject Pageable pageable) { return helloCallerService.callHelloServiceClient(example, pageable); }
当我尝试像这个客户端接口那样通过伪装调用类似的控制器接口时:
When I try to call a similar controller interface via feign like this client interface:
@PostMapping("/search") public Page<HelloDto> searchHellos(@RequestHeader("apiKey") String apiKey, @RequestBody HelloDto example, Pageable pageable);
应用程序不会启动,因为该方法有太多的主体参数".我试图用 @RequestParam 注释 Pageable 但它不会被识别/解析.
The application won't start since the method "has too many body params". I tried to annotate Pageable with @RequestParam but it won't be recognized / parsed.
我的问题是:
我们可以使用自定义 AnnotatedParameterProcessor 例如@ParameterObject 将 ParameterObject 弄平并将其编码为 url 查询参数?
Can we use a custom AnnotatedParameterProcessor for e.g. @ParameterObject that flats out the ParameterObject and encodes it into the url query parameters?
你会怎么做?或者 AnnotatedParameterProcessor 是错误的方式?我可能需要扩展器或任何东西吗?
How would you do that? Or is the AnnotatedParameterProcessor the wrong way? Do I maybe need an expander or anything?
推荐答案
你的问题比 springdoc-openapi 更与 feign 用法相关.使用 org.springframework.cloud.openfeign.SpringQueryMap
,解决您的问题.
Your question is more related to feign usage than springdoc-openapi.
Using using org.springframework.cloud.openfeign.SpringQueryMap
, solves your problem.
这篇关于如何将 Spring Pageable 传递给 FeignClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!