0

我正在尝试编写一个 feign 客户端来调用以从服务器检索数据,其中 api 接受相同命名的查询参数列表以确定要询问多少数据。这是我试图点击的示例网址:

http://some-server/some-endpoint/{id}?include=profile&include=account&include=address&include=email

到目前为止,对于我的假客户,我正在尝试以这种方式进行设置:

@FeignClient("some-server")
public interface SomeServerClient {
  @RequestMapping(method = RequestMethod.GET,
      value = "/customers/api/customers/{id}",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  Map<Object, Object> queryById(
      @PathVariable long id,
      @RequestParam("include[]") String ... include);

  default Map<Object, Object> queryById(long id) {
    return queryById(id,"profile", "account", "address", "email");
  }

但是,这似乎并没有以所需的方式格式化请求,所以我的问题是如何设置我的 feign 客户端以将其请求提交到 url,如上例所示?

4

1 回答 1

1

使用@RequestParam("include") List<String> includes,例如:

客户

@FeignClient(value = "foo-client")
public interface FooClient {

    @GetMapping("/foo")
    Foo getFoo(@RequestParam("include") List<String> includes);

}

控制器

@RestController
public class FooController {

    @GetMapping("/foo")
    public Foo getFoo(@RequestParam("include") List<String> includes) {
        return new Foo(includes);
    }

}

用法

List<String> includes = new ArrayList<>();
        includes.add("foo");
        includes.add("bar");

        Foo foo = fooClient.getFoo(includes);

网址

http://some-server/foo?include=foo&include=bar
于 2019-11-26T20:23:12.427 回答