如何使用 feign 发送一个空列表作为参数?

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

我想发送一个列表作为参数当它是空的时也可以假装。

目前在feign client我有以下方法:

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

问题是当 genre 为空列表时,feign 创建请求 GET /artists 而不是 GET /artists?genres=

我正在尝试对多个请求使用相同的 uri /artists,仅通过查询参数来区分它们,例如 GET /artists?genre=1,2,4 GET /artists?popularity=89 全部映射到不同的方法。

如何使用 feign 发送空列表作为参数?

您遇到的是 empty 参数和 undefined 参数之间的区别。 Feign 在扩展 uri 参数时最好遵守 RFC 6570 - Uri Templates。目前,仅支持简单扩展。规范指出,在处理未定义的值和列表时:

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.

通过提供一个空列表,根据定义,undefinedundefined 值将被忽略,导致参数根本不被包含。如果要包含参数,无论列表是否包含任何值,都需要更新 uri 模板:

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

即使列表为空,这也会在请求中包含 genre=