gpt4 book ai didi

java - Spring Boot GET 请求 API

转载 作者:行者123 更新时间:2023-12-01 19:27:49 25 4
gpt4 key购买 nike

所以,我有这个JSON data这是原始格式。它只是表示当前世界各地每天的 covid-19 记录。我需要向它发送 GET 请求并使用 Spring Boot 在浏览器中显示数据。我尝试了 getForObject(url, class) 方法,但它给出了一个错误,并显示消息 没有找到适合响应类型的 HttpMessageConverter。我尝试解决它但无法解决。然后我尝试了使用 JSON 数据的 URL 和 Covid19.class 的 ObjectMapper.readValue(url, class) 方法。这次,我收到一条错误消息 no protocol。以下是该项目的结构:

Covid19.java:

public class Covid19 implements Serializable {
private final String country;

public Covid19(String country){
this.country = country;
}

public String getCountry(){
return country;
}
}

Covid19Controller.java:

@RestController
public class Covid19Controller {
@GetMapping(value = "/covid", produces = MediaType.APPLICATION_JSON_VALUE)
public Covid19 covid19() throws IOException {
URL url = new URL("raw.githubusercontent.com/pomber/covid19/master/docs/timeseries.json");
Covid19 covid19 = new ObjectMapper().readValue(url, Covid19.class);
return covid19;
}

主类:

public static void main(String[] args) {
SpringApplication.run(DataminingWebserviceApplication.class, args);
}

实际上我的目标是发送一个带有参数country的GET请求,但一开始就卡住了。我昨天开始学习Spring Boot,并努力学习它。有很多教程,但没有一个非常适合我的情况。提前致谢。

最佳答案

使用该模型来映射数据

public class Covid19 {
String date;
Integer confirmed;
Integer recovered;
Integer deaths;
... setter getter
}

并读取数据使用

    final String uri = "https://raw.githubusercontent.com/pomber/covid19/master/docs/timeseries.json";
ObjectMapper objectMapper = new ObjectMapper();
Map<String, List<Covid19>> countryMap = objectMapper.readValue(uri,new TypeReference<Map<String, List<Covid19>>>(){});

关于java - Spring Boot GET 请求 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60954202/

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