gpt4 book ai didi

java - JSON/REST 中的 ObjectMapper 出现 FileNotFoundException

转载 作者:行者123 更新时间:2023-12-02 06:41:26 25 4
gpt4 key购买 nike

我创建了一个类,它提供了一种通过 REST 获取 JSON 对象的方法,然后将该对象反序列化为 LinkedHashMap。方法如下:

 @Override
public LinkedHashMap refreshStatus(String metricType, String metricPeriod) {
ObjectMapper mapper = new ObjectMapper();
LinkedHashMap<String,String> map = null;

try {
map = mapper.readValue(new File("http://localhost:8081/Metrics/Stats/JSONP/ReceiveData/"+metricType+"/"+metricPeriod+"/?callback="), LinkedHashMap.class);


} catch (JsonParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return map;
}

当我在浏览器中输入带有一些特定参数的 URL 时,即

http://localhost:8081/Metrics/Stats/JSONP/ReceiveData/Totals/All/?callback=

我得到了一个正确的 JSON 对象:

{"Boxes":"8","Contracts":"23","Updates":"1","Deactivations":"4","ActiveBoxes":"33","InactiveBoxes":"1","PendingInstallationsContracts":"70","Alerts":"626","PercentsBoxInstalled":"100.0%","Miles":"0"}

但是,当我运行应用程序并调用该方法时,我收到 FileNotFOudException:

java.io.FileNotFoundException: http:/localhost:8081/Metrics/Stats/JSONP/ReceiveData/Totals/All/?callback= (No such file or directory)

有谁知道问题出在哪里吗?预先感谢!

最佳答案

您的计算机上确实找不到此文件。您尝试在本地文件系统中创建与实际文件相对应的文件对象,并提供 URL 作为参数而不是文件系统路径。

您实际上想要执行 HTTP GET,即执行如下操作。

urlString = "http://localhost:8081/Metrics/Stats/JSONP/ReceiveData/Totals/All/?callback=";
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
map = mapper.readValueconn.getInputStream();

关于java - JSON/REST 中的 ObjectMapper 出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19097521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com