如何在FeignClient中调用多个查询字符串参数的url?

jer*_*eca 5 spring spring-boot feign

我尝试使用多个查询字符串参数调用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);

}
Run Code Online (Sandbox Code Playgroud)

问题是'&'的特征RequestMapping value被替换为&

怎么避免这个?

谢谢 !

Ale*_*can 10

所有查询参数将通过使用该&字符的拆分自动从URL中提取,并映射到方法声明中的相应@RequestParam.因此,您不需要指定@RequestMapping注释的所有键,只应指定端点值.

为了您的示例,您只需将rest-endpoint更改为

@RequestMapping(method=RequestMethod.GET)
GoogleMatrixResult process(@RequestParam(value="key") String key,
                           @RequestParam(value="origins") String origins,
                           @RequestParam(value="destinations") String destinations);
Run Code Online (Sandbox Code Playgroud)

  • 我花了几个小时才意识到 Spring Boot 需要 openfeign 9.7.0,而不是 10.0.1 (否则你会收到关于 jar 中缺少功能的错误) (2认同)