简体   繁体   English

如何使用 FeignClient 获取 api

[英]How can I get api with FeignClient

I used Lombok, Open Feign and Spring Web我使用了 Lombok、Open Feign 和 Spring Web

I have currencyClient interface:我有 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);
}

And Controller:和控制器:

@RestController
@RequiredArgsConstructor
public class StatusController {

private String appId1 = "appId";
private final currencyClient currencyClient;

@GetMapping("/getAllCurrency")
public List<Object> getCurrency(){
    return currencyClient.getCurrency(appId1);
}

}

And " http://localhost:1212/getAllCurrency " is not working cause the link is converted into "** 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.并且“ http://localhost:1212/getAllCurrency ”不起作用,因为链接被转换为“** https://openexchangerates.org/api/historical/2012-07-10.json/appId**”我明白了那 &/= 被颠倒了,我也认为我对 List 的指示是不正确的。 That's what I tried so how can I get info from "** https://openexchangerates.org/api/historical/2012-07-10.json?app_id**" as " http://localhost:1212/getAllCurrency "?这就是我尝试过的,所以我如何从“** https://openexchangerates.org/api/historical/2012-07-10.json?app_id**”作为“ http://localhost:1212/getAllCurrency ”获取信息?

According to the https://docs.openexchangerates.org docs, the app_id should be a request parameter (see @RequestParam ), not a path variable.根据https://docs.openexchangerates.org文档, app_id应该是请求参数(请参阅@RequestParam ),而不是路径变量。 You could do something like this:你可以这样做:

CurrencyClient interface: 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 : 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);
    }
}

Some additional things to note here:这里还有一些需要注意的事项:

Please don't post yout API key to StackOverflow, or anywhere else publicly.请不要将您的 API 密钥发布到 StackOverflow 或其他任何公开的地方。 Other people might abuse it.其他人可能会滥用它。 Since you already posted it, you should request a new API key and get rid of this one (close it if possible).既然您已经发布了它,您应该请求一个新的 API 密钥并摆脱这个(如果可能的话关闭它)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM