【问题标题】:How to write a feign client for an api that has multiple identically named query params?如何为具有多个同名查询参数的 api 编写 feign 客户端?
【发布时间】:2020-03-22 05:33:30
【问题描述】:

我正在尝试编写一个 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?

【问题讨论】:

    标签: java spring-boot openfeign


    【解决方案1】:

    使用@RequestParam("include") List&lt;String&gt; 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
    

    【讨论】:

      猜你喜欢
      • 2020-03-15
      • 2017-03-07
      • 1970-01-01
      • 2020-12-20
      • 2018-12-01
      • 2020-01-14
      • 2020-04-05
      • 2020-01-02
      • 2016-03-09
      相关资源
      最近更新 更多