java - 我们可以在没有 API ID 的情况下调用外部 API 吗?
问题描述
我是 API 设计的新手,我正在做一个项目,我需要从波兰国家银行 http://api.nbp.pl调用货币兑换 API,但我没有看到任何可以找到 API ID 的迹象。如果我尝试在没有 API ID 的情况下运行应用程序,则会在 Spring Boot 上进行此开发,它会引发 404 错误。
这是我写的一段代码。
@RequestMapping(method = RequestMethod.GET, value = "/exchangerates/rates/{table}/{code}")
public @ResponseBody Object getAllCurriencyExchangeRates(@PathVariable String table, @PathVariable String code) {
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
ResponseEntity<Object> response =
restTemplate.getForEntity("http://api.nbp.pl/api/" +table+ "," +code+ Object.class, null, headers);
return response;
}
实际查询http://api.nbp.pl/api/exchangerates/rates/a/chf/
所以,我的问题是我们可以调用没有 API ID 的外部 API 吗?
解决方案
首先,您试图访问错误的 API。这就是为什么您找不到 404 的原因。404 表示没有像你打电话的网址。
仔细检查你的restTemplate,
restTemplate.getForEntity("http://api.nbp.pl/api/" + table+ "," +code+ Object.class, null, headers);
连接字符串时你做错了。它应该看起来像这样;
restTemplate.getForEntity("http://api.nbp.pl/api/exchangerates/rates/"+table+"/"+code, Object.class, null, headers);
给 API 开发者一个提示,首先你应该使用 Postman 玩 api,然后用 api 编写代码。
推荐阅读
- javascript - Custom jQuery UI that doesn't have dialog and modal, needs a background overlay under the dialog box
- c# - EF Core 3.1:序列不包含任何元素,使用 FromSqlRaw 查询存储过程
- go - 去获取私有 bitbucket repo,禁止 403
- javascript - Outlook 加载项的基于边缘的 HTML 渲染器:window.open() 弹出窗口缺少最大化按钮
- java - 类内方法外加法
- javascript - 我正在尝试使用带有 getElementById 的外部 Javascript 修改 HTML 元素的内容,但它不起作用
- javascript - 在 HTML Canvas 上绘制动态创建的 SVG
- linux - 我得到'bash:cd:太多参数',我不知道为什么?
- eclipse - 是否有可能在 Eclipse 中有一个受密码保护的更新站点?
- python - 我可以使用 tkinter 在两列内创建不同的行号吗?