Search code examples
javaspring-bootopenfeign

How to write a feign client for an api that has multiple identically named query params?


I am trying to write a feign client to make calls to retrieve data from a server where the api accepts a list of identical named query parameters to determine how much data is being asked. Here is an example url I am trying to hit:

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

So far for my feign client I'm attempting to set it up this way:

@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");
  }

However this doesn't appear t format the request in the way desired, so my question is how can I set up my feign client to submit its request to the url as shown in the example above?


Solution

  • use @RequestParam("include") List<String> includes, example:

    client:

    @FeignClient(value = "foo-client")
    public interface FooClient {
    
        @GetMapping("/foo")
        Foo getFoo(@RequestParam("include") List<String> includes);
    
    }
    

    controller:

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

    usage:

    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