Feign Client中不支持Spring Data Pageable作为RequestParam

编程入门 行业动态 更新时间:2024-10-16 00:21:45
本文介绍了Feign Client中不支持Spring Data Pageable作为RequestParam的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在尝试为我的其余api公开一个Feign Client.它以Pageable作为输入,并定义了PageDefaults.

I have been trying to expose a Feign Client for my rest api. It takes Pageable as input and has PageDefaults defined.

控制器:

@GetMapping(value = "data", produces = MediaType.APPLICATION_JSON_VALUE) @ApiOperation(value = "Get Data", nickname = "getData") public Page<Data> getData(@PageableDefault(size = 10, page = 0) Pageable page, @RequestParam(value = "search", required = false) String search) { return service.getData(search, page); }

这是我的假客户:

@RequestMapping(method = RequestMethod.GET, value = "data") public Page<Data> getData(@RequestParam(name = "pageable", required = false) Pageable page, @RequestParam(name = "search", defaultValue = "null", required = false) String search);

现在问题是无论我发送给Feign Client的页面大小和页码如何,它始终会应用PageDefaults(0,10).

Now the problem is regardless of whatever page size and page number I send to Feign Client it always applies PageDefaults (0,10).

当我直接致电休息服务时,它会起作用: localhost:8080/data?size = 30& page = 6

When I call the rest service directly it works: localhost:8080/data?size=30&page=6

我正在使用Spring Boot 2.1.4.RELEASE和Spring Cloud Greenwich.SR1.最近进行了一项修复,以支持Pageable( github/spring-cloud/spring-cloud-openfeign/issues/26#issuecomment-483689346 ).但是,我不确定以上情况是否会解决,或者我丢失了某些内容.

I am using Spring Boot 2.1.4.RELEASE and Spring Cloud Greenwich.SR1. Recently a fix was done to support Pageable (github/spring-cloud/spring-cloud-openfeign/issues/26#issuecomment-483689346). However I am not sure it the above scenario is not covered or I am missing something.

推荐答案

我认为您的代码不起作用,因为您在Feign方法中对Pageable参数使用了@RequestParam注释.

I think your code doesn't work because you are using @RequestParam annotation for Pageable parameter in your Feign method.

我对这种方法的实现按预期工作.

My implementation of such a method works as expected.

客户:

@FeignClient(name = "model-service", url = "localhost:8080/") public interface ModelClient { @GetMapping("/models") Page<Model> getAll(@RequestParam(value = "text", required = false) String text, Pageable page); }

控制器:

@GetMapping("/models") Page<Model> getAll(@RequestParam(value = "text", required = false, defaultValue = "text") String text, Pageable pageable) { return modelRepo.getAllByTextStartingWith(text, pageable); }

请注意,在我的情况下,Spring没有公开PageJacksonModule作为bean,而是引发了异常:

Note that in my case, without exposing PageJacksonModule as a bean, Spring raised the exception:

InvalidDefinitionException:无法构造org.springframework.data.domain.Page的实例

所以我不得不将其添加到项目中:

So I had to add it to the project:

@Bean public Module pageJacksonModule() { return new PageJacksonModule(); }

我的工作演示: github/Cepr0/sb-feign -client-with-pageable-demo

更多推荐

Feign Client中不支持Spring Data Pageable作为RequestParam

本文发布于:2023-11-12 19:49:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582372.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中不   Client   Feign   Spring   RequestParam

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!