How to pass list of parameters using Spring Feign

We can use Spring Feign to connect to external APIs. Feign makes is very easy to connect to external APIs by providing a declarative approach and encapsulating lots of connection logic.

In this article we will discuss about how we can pass a list of values as a URL Query parameter to an api.

A webservice URL can accept a list as a parameter in one of the below two formats.

1. Repeated parameter name

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


2. Comma Separated Values

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

 

In general case, an API coded in one of the above formats can not accept parameters in the other format. As a consumer of these APIs we can not control the format used by the API. Feign provides simple solutions to choose between these types.

1. Repeated parameter name

To pass a list as repeated Query parameters, simply define the Feign methods to accept parameter as a List object. 
Example


2. Comma Separated Values

To pass a list as comma separated values in URL query parameter, we need to define the parameter as an array in our Feign client.

The feign client will create appropriate URL format based on the parameter type.

0 comments:

Post a Comment