Spring Cloud Feign Client @RequestParam with List parameter creates a wrong request

[亡魂溺海] 提交于 2021-01-02 06:06:29

问题


I have a Spring Clound Feign Client mapping defined as following

@RequestMapping(method = RequestMethod.GET, value = "/search/findByIdIn")
Resources<MyClass> get(@RequestParam("ids") List<Long> ids);

when I call

feignClient.get(Arrays.asList(1L,2L,3L))

according to what I can see in the debugger, the feign-core library forms the following request:

/search/findByIdIn?ids=1&ids=2&ids=3

instead of expected

/search/findByIdIn?ids=1,2,3

which would be correct for the server Spring Data REST endpoint declared in the same way as my Feign client method.

Thus, because of this issue, the request always returns empty set.

I have seen similar question, but it looks like the Feign client was working as I expect back in 2015.

I am using:

  • spring-cloud-starter-feign version 1.2.4.RELEASE
  • feign-httpclient version 9.4.0
  • feign-core version 9.4.0

Is there a way to correct the behaviour and "marry" the Spring Cloud Feign Client with the Spring Data REST defined endpoints?


回答1:


I had the same issue with multiple occurence of the parametre instead of the expected comma separated sequence of items. The solution was really simple:

In my feign client I used arrays

feignClient.get(new Long[]{1L,2L,3L})

instead of collection/list:

feignClient.get(Arrays.asList(1L,2L,3L))




回答2:


In Feign you can annotate your controller with the following

@CollectionFormat(feign.CollectionFormat.CSV) and it will process collections in

the CSV format findByIdIn?ids=1&ids=2&ids=3



来源:https://stackoverflow.com/questions/41744542/spring-cloud-feign-client-requestparam-with-list-parameter-creates-a-wrong-requ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!