如何使用FeignClient获取api



我使用了Lombok、Open Feign和Spring Web

我有currencyClient接口:

@FeignClient(value = "getcurrency", url = "https://openexchangerates.org")
public interface currencyClient {
@RequestMapping(value = "/api/historical/2012-07-10.json/{smt}", method = RequestMethod.GET)
public List<Object> getCurrency(@PathVariable String smt);
}

控制器:

@RestController
@RequiredArgsConstructor
public class StatusController {
private String appId1 = "appId";
private final currencyClient currencyClient;
@GetMapping("/getAllCurrency")
public List<Object> getCurrency(){
return currencyClient.getCurrency(appId1);
}
}

并且">http://localhost:1212/getAllCurrency";不起作用,因为链接被转换为"**https://openexchangerates.org/api/historical/2012-07-10.json/appId**"我理解&/=相反,我也认为我对名单的指示是不正确的。这就是我所尝试的,所以我如何才能从"**https://openexchangerates.org/api/historical/2012-07-10.json?app_id**"作为">http://localhost:1212/getAllCurrency";?

根据https://docs.openexchangerates.org文档中,app_id应该是一个请求参数(请参阅@RequestParam),而不是一个路径变量。你可以这样做:

CurrencyClient接口:

@FeignClient(value = "getcurrency", url = "https://openexchangerates.org")
public interface CurrencyClient {
@RequestMapping(value = "/api/historical/2012-07-10.json", method = RequestMethod.GET)
Map<String, Object> getCurrency(@RequestParam("app_id") String appId);
}

StatusController:

@RestController
public class StatusController {
private final CurrencyClient currencyClient;
public MyController(CurrencyClient currencyClient) {
this.currencyClient = currencyClient;
}
@GetMapping("/getAllCurrency")
public Map<String, Object> getCurrency() {
String appId1 = "*****";
return currencyClient.getCurrency(appId1);
}
}

这里需要注意的一些附加事项:

请不要将您的API密钥发布到StackOverflow或任何其他公开位置。其他人可能会滥用它。由于你已经发布了它,你应该请求一个新的API密钥并丢弃这个密钥(如果可能,请关闭它)。

相关内容

  • 没有找到相关文章

最新更新