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

时间:2019-05-07 11:11:39

标签: 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);
}

这是我的假客户:

@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)。但是,我不确定上述情况是否没有解决,或者我丢失了某些东西。

1 个答案:

答案 0 :(得分:1)

我认为您的代码无效,因为您在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);
}

请注意,就我而言,Spring没有将PageJacksonModule公开为bean,而是引发了异常:

  

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

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

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

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