Gau*_*rma 6 spring spring-data spring-cloud spring-cloud-feign feign
我一直在尝试为我的其余 api 公开一个 Feign Client。它采用 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);
}
Run Code Online (Sandbox Code Playgroud)
这是我的假客户:
@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);
Run Code Online (Sandbox Code Playgroud)
现在的问题是,无论我发送给 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);
}
Run Code Online (Sandbox Code Playgroud)
控制器:
@GetMapping("/models")
Page<Model> getAll(@RequestParam(value = "text", required = false, defaultValue = "text") String text, Pageable pageable) {
return modelRepo.getAllByTextStartingWith(text, pageable);
}
Run Code Online (Sandbox Code Playgroud)
请注意,在我的例子中,Spring 没有公开PageJacksonModule为 bean,而是引发了异常:
InvalidDefinitionException:无法构造实例
org.springframework.data.domain.Page
所以我必须将其添加到项目中:
@Bean
public Module pageJacksonModule() {
return new PageJacksonModule();
}
Run Code Online (Sandbox Code Playgroud)
我的工作演示:github.com/Cepr0/sb-feign-client-with-pageable-demo
| 归档时间: |
|
| 查看次数: |
9296 次 |
| 最近记录: |