spring - Feign Client 中的 RequestParam 不支持 Spring Data Pageable
问题描述
我一直在尝试为我的 rest api 公开一个 Feign 客户端。它将 Pageable 作为输入并定义了 PageDefaults。
控制器:
@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)。
当我直接调用其余服务时,它可以工作: http://localhost:8080/data?size=30&page=6
我正在使用 Spring Boot 2.1.4.RELEASE 和 Spring Cloud Greenwich.SR1。最近进行了修复以支持 Pageable ( https://github.com/spring-cloud/spring-cloud-openfeign/issues/26#issuecomment-483689346 )。但是,我不确定是否没有涵盖上述情况,或者我遗漏了一些东西。
解决方案
我认为您的代码不起作用,因为您在 Feign 方法中使用@RequestParam
了参数注释。Pageable
我对这种方法的实现按预期工作。
客户:
@FeignClient(name = "model-service", url = "http://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);
}
请注意,在我的例子中,没有暴露PageJacksonModule
为 bean,Spring 引发了异常:
InvalidDefinitionException:无法构造实例
org.springframework.data.domain.Page
所以我不得不将它添加到项目中:
@Bean
public Module pageJacksonModule() {
return new PageJacksonModule();
}
推荐阅读
- c# - 我使用 html.beginform 从控制器的视图发送信息,它改变了页面的整个视图 - ASP.NET
- android - Android 媒体播放器 - 无法设置数据源 - URL
- html - 为什么我的网格单元没有占用它们分配的 fr 单元?
- java - Android Studio 问题:W/RecyclerView:未连接适配器;跳过布局
- c - 使用链表下降优先级队列而不以优先级作为输入
- python - 在 tkinter 中引入新小部件时将小部件保留在其位置
- jakarta-ee - 如何使用 Quarkus 在生产环境中禁用从 /resources/META-INF/resources 访问某些静态文件?
- python - Python多处理运行过程两次?
- javascript - 如何让 JSON 数据返回?
- c++ - 在 gcc 路径搜索中 - 我无法在当前目录中导入文件