如何为具有多个同名查询参数的api编写伪客户端?

时间:2019-11-26 18:09:49

标签: java spring-boot openfeign

我正在尝试编写一个伪客户端来调用从服务器检索数据的服务器,其中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");
  }

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

1 个答案:

答案 0 :(得分: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);

url

http://some-server/foo?include=foo&include=bar