如何使用 FeignClient 获得 api

How can I get api with FeignClient

我使用了 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”不起作用,因为 link 被转换为“**https://openexchangerates.org/api/historical/2012-07-10.json/appId**" I understand that &/= are reversed and I also think that my indication of List is not correct. That's what I tried so how can I get info from "**https://openexchangerates.org/api/historical/2012-07-10.json?app_id**" 作为” http://localhost:1212/getAllCurrency"?

根据 https://docs.openexchangerates.org docs, the app_id should be a request parameter (see @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);
    }
}

这里需要注意的一些额外事项:

请不要 post 您 API Whosebug 或其他任何公开的密钥。其他人可能会滥用它。由于您已经 post 编辑了它,因此您应该请求一个新的 API 密钥并删除该密钥(如果可能,将其关闭)。