Search code examples
springspring-cloud-feignfeign

How to send an empty list as a parameter with feign?


I would like to send a list as a parameter also when it is empty with feign.

Currently at the feign client I have the following method:

@GetMapping("/artists")
List<Artist> getArtists(@RequestParam("genre") List<Long> genre);

The problem with it is that when genre is an empty list, feign creates the request GET /artists instead of GET /artists?genres=

I'm trying to use use the same uri /artists for multiple requests, differentiating them only by the query parameters, like GET /artists?genre=1,2,4 GET /artists?popularity=89 all get mapped to different methods.

How can I send an empty lists as a parameter with feign?


Solution

  • What you are experiencing is the difference between empty parameters and undefined parameters. Feign does it's best to adhere to RFC 6570 - Uri Templates, when expanding uri parameters. At the moment, only Simple Expansion is supported. The specification states that, when dealing with undefined values and lists:

    2.3 Variables

    A variable defined as a list value is considered undefined if the list contains zero members.

    3.2.1 Variable Expansion

    A variable that is undefined (Section 2.3) has no value and is ignored by the expansion process. If all of the variables in an expression are undefined, then the expression's expansion is the empty string.

    By providing an empty list, it is, by definition, undefined and undefined values ignored, resulting in the parameter not being included at all. If you want to include the parameter, regardless of if the list contains any values, you will need to update the uri template:

    @GetMapping("/artists?genre=")
    List<Artist> getArtists(@RequestParam("genre") List<Long> genre);
    

    This will include genre= on the request even if the list is empty.