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

ⅰ亾dé卋堺 提交于 2019-12-23 12:14:42

问题


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 !


回答1:


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 destinations);



回答2:


**use this:-

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

then form url say:-
http://localhost:portnumber/.../key-value/origins-value/destinations-value and hit this url, I am sure it will work for you using @PathVariable annotations**



来源:https://stackoverflow.com/questions/40970498/how-to-call-url-with-multiple-query-string-params-in-feignclient

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!