简体   繁体   English

我们可以在没有 API ID 的情况下调用外部 API 吗?

[英]Can we call external API without API ID?

I am new to API design, I am working on one project where I need to call currency exchange API from National Bank of Poland http://api.nbp.pl but I do not see any indication where I can find API ID.我是 API 设计的新手,我正在做一个项目,我需要从波兰国家银行http://api.nbp.pl调用货币兑换 API,但我没有看到任何可以找到 API ID 的指示。 This development is on Spring Boot if I am trying to run the application without API ID it is throwing 404 error.如果我尝试在没有 API ID 的情况下运行应用程序,则此开发是在 Spring Boot 上进行的,它会引发 404 错误。

Here is the piece of code that I have written.这是我写的一段代码。

@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;
}        

Actual query http://api.nbp.pl/api/exchangerates/rates/a/chf/实际查询http://api.nbp.pl/api/exchangerates/rates/a/chf/

So, my question is can we call an external API without API ID?所以,我的问题是我们可以在没有 API ID 的情况下调用外部 API 吗?

First things first, you are trying to reach wrong API.首先,您试图访问错误的 API。 That is why you are getting 404 not found.这就是为什么您找不到 404 的原因。 404 means there is no url like you are calling. 404 表示没有您正在调用的网址。

Check your restTemplate carefully,仔细检查你的restTemplate,

restTemplate.getForEntity("http://api.nbp.pl/api/" + table+ "," +code+ Object.class, null, headers);

You are doing wrong when concatenate strings.连接字符串时您做错了。 It should look something like this;它应该看起来像这样;

restTemplate.getForEntity("http://api.nbp.pl/api/exchangerates/rates/"+table+"/"+code, Object.class, null, headers);

And a hint for API developers, firstly you should play with api using Postman and then write code with api.给 API 开发者的一个提示,首先你应该使用 Postman 玩 api,然后使用 api 编写代码。

Try this - I have tested it - it works.试试这个 - 我已经测试过了 - 它有效。 Please keep in mind this is just a test implementation.请记住,这只是一个测试实现。 Things inside main method have to be copied into your getAllCurriencyExchangeRates method.必须将main方法中的内容复制到getAllCurriencyExchangeRates方法中。 And for sure replace "a" and "chf" through variables.并且肯定会通过变量替换"a""chf" I assume table and code are the variables you want to use.我假设tablecode是您要使用的变量。 I used String because I don't know which type of object you want to return.我使用String是因为我不知道您要返回哪种类型的对象。 You can use your own pojo for sure instead of String .您肯定可以使用自己的 pojo 而不是String

package scripts;

import java.net.URI;

import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * author: flohall
 * date: 08.12.19
 */
public class Test {

    public static void main(final String[] args){
        final String url = "http://api.nbp.pl/api/exchangerates/rates";
        final URI uri = UriComponentsBuilder.fromHttpUrl(url).path("/").path("a").path("/").path("chf").build().toUri();
        System.out.println(uri);

        final RestOperations restTemplate = new RestTemplate();
        final ResponseEntity<String> result = restTemplate.getForEntity(uri, String.class);

        System.out.println(result.getBody());
    }

}

Try with this试试这个

ResponseEntity<Object> response =
            restTemplate.getForEntity("http://api.nbp.pl/api/exchangerates/rates/" + table + "/" + code, Object.class, headers);

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

相关问题 我们如何在没有正在进行的交易的情况下拨打外部电话? - How can we make an external call without an ongoing transaction? 我们可以在没有互联网/没有调用 api 调用 mapbox 的情况下在 Mapbox 地图上获得路线吗? - Can we get Route on Mapbox map without internet/withpout calling api call to mapbox? 我们如何调用带有多个参数的spring API? - How can we call the spring API with multiple parameters? 我们如何在 Java 中进行异步 REST api 调用? - How can we make asynchronous REST api call in Java? 带回调的外部API异步调用 - External API async call with callback 在Outlook中触发外部API调用,以回复来自特定电子邮件ID的电子邮件 - Trigger an external API call in Outlook replying to an email coming from a specific email-id 我可以返回 API 响应而不等待 Spring 引导中的其他外部 API 调用吗? - Can I return an API response without waiting on other external API calls in Spring Boot? 我们可以在Java页面中调用任何视图而不调用其ID吗? - Can we call any view in java page without calling its id? 如何在Spring Boot中测试对外部api的调用 - How to test a call to external api in Spring Boot 外部 api 调用 apache 光束数据流 - external api call in apache beam dataflow
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM