Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call url with multiple query string params in FeignClient?

I try to call Google API with multiple query string parameters. And curiously, I can't find a way to do that.

This is my FeignClient :

@FeignClient(name="googleMatrix", url="https://maps.googleapis.com/maps/api/distancematrix/json")
public interface GoogleMatrixClient {

    @RequestMapping(method=RequestMethod.GET, value="?key={key}&origins={origins}&destinations={destinations}")
    GoogleMatrixResult process(@PathVariable(value="key") String key,
                               @PathVariable(value="origins") String origins,
                               @PathVariable(value="destinations") String destinations);

}

The problem is that '&' character of the RequestMapping value is replace by &

How to avoid this ?

Thanks !

like image 808
jeremieca Avatar asked Dec 05 '16 09:12

jeremieca


People also ask

How do you pass query parameters in feign client?

Query parameters can be configured in Feign clients by using the @RequestParam annotation from the Spring web framework on method arguments which should be passed as query parameters when calling the remote service.

How do you pass a URL in feign client?

Feign has a way to provide the dynamic URLs and endpoints at runtime. The following steps have to be followed: In the FeignClient interface we have to remove the URL parameter. We have to use @RequestLine annotation to mention the REST method (GET, PUT, POST, etc.):

What is @FeignClient in spring boot?

Overview. FeignClient is a Declarative REST Client in Spring Boot Web Application. Declarative REST Client means you just give the client specification as an Interface and spring boot takes care of the implementation for you. FeignClient is used to consume RESTFul API endpoints exposed by thirdparty or microservice.


1 Answers

All Query parameters will automatically be extracted from the url by a split using the & character and mapped to the corresponding @RequestParam in the method declaration.

So you don't need to specify all the keys the @RequestMapping annotation and there you should only specify the endpoint value.

For your example to work you just need to change your rest-endpoint to:

@RequestMapping(method=RequestMethod.GET)
GoogleMatrixResult process(@RequestParam(value="key") String key,
                           @RequestParam(value="origins") String origins,
                           @RequestParam(value="destinations") String destin);
like image 59
Alex Ciocan Avatar answered Nov 02 '22 10:11

Alex Ciocan