【问题标题】:How to call url with multiple query string params in FeignClient?如何在 FeignClient 中使用多个查询字符串参数调用 url?
【发布时间】:2017-04-19 14:59:08
【问题描述】:

我尝试使用多个查询字符串参数调用 Google API。奇怪的是,我找不到这样做的方法。

这是我的 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);

}

问题是RequestMapping value 的“&”字符被& 替换

如何避免这种情况?

谢谢!

【问题讨论】:

    标签: spring spring-boot feign


    【解决方案1】:

    所有 Query 参数将通过使用 & 字符的拆分自动从 url 中提取,并映射到方法声明中对应的 @RequestParam

    所以你不需要在@RequestMapping注解中指定所有的键,你应该只指定端点值。

    为了让您的示例正常工作,您只需将休息端点更改为:

    @RequestMapping(method=RequestMethod.GET)
    GoogleMatrixResult process(@RequestParam(value="key") String key,
                               @RequestParam(value="origins") String origins,
                               @RequestParam(value="destinations") String destin);
    

    【讨论】:

    • 好的,完美,我测试了很多东西,但不是这个!
    • 花了我几个小时才意识到 Spring Boot 需要 openfeign 9.7.0,而不是 10.0.1(否则你会收到关于 jar 中缺少函数的错误)
    【解决方案2】:

    **使用这个:-

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

    然后形成网址 说:-
    http://localhost:portnumber/.../key-value/origins-value/destinations-value 并点击此网址,我相信它会为您使用 @PathVariable 注释**

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-05
      • 1970-01-01
      • 2018-04-20
      • 1970-01-01
      • 2011-08-26
      • 2010-10-17
      • 2014-01-24
      相关资源
      最近更新 更多