【问题标题】:Spring Boot GET request to API对 API 的 Spring Boot GET 请求
【发布时间】:2020-07-12 05:13:58
【问题描述】:

所以,我有这个JSON data,它是原始格式的。它只是表示每天在世界范围内当前的 covid-19 记录。我需要向它发送一个 GET 请求并使用 Spring Boot 在浏览器中显示数据。我尝试过getForObject(url, class) 方法,但它给出了一条错误消息no suitable HttpMessageConverter found for response type。我试图解决它,但不能。然后我用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并尝试学习它。有很多教程,但没有一个非常适合我的情况。提前致谢。

【问题讨论】:

    标签: java json spring spring-boot jackson


    【解决方案1】:

    你定义的类不正确,创建一个这样的类

    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Covid19{
        private int confirmed;
        private int deaths;
         ....
        // add other fields and Getters & Setters
    }
    

    读取数据的代码应该是这样的

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

    covid19Map 的键为国家,值为 json 中指示的日期列表

    【讨论】:

      【解决方案2】:

      使用 HTTP 协议访问 URL:

      URL("http://raw.githubusercontent.com/pomber/covid19/master/docs/timeseries.json");
      
      

      另外,使用以下代码获取数据

      URL hh= new URL("http://raw.githubusercontent.com/pomber/covid19/master/docs/timeseries.json");
              URLConnection connection = hh.openConnection();
      /*
      If your URL is redirected to other URL. It's quite normal and web site may have many reasons to redirect you. Just follow the redirect based on "Location" HTTP header like that:
      */
              String redirect = connection.getHeaderField("Location");
              if (redirect != null){
                  connection = new URL(redirect).openConnection();
              }
              BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
              String inputLine;
              System.out.println();
              while ((inputLine = in.readLine()) != null) {
                  System.out.println(inputLine);
              }
      

      此外,要通过您的 API 发送数据,请在您的 POJO 类中进行更改并相应地返回 Object。

      【讨论】:

        【解决方案3】:

        使用此模型映射数据

        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>>>(){});
        

        【讨论】:

        • 我遇到了同样的错误cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (URL); line: 4, column: 7] (through reference chain: java.util.LinkedHashMap["Afghanistan"]-&gt;java.util.ArrayList[0])
        • 在 Model 类中也使用 Setter Getter 和 Constructor
        • 对不起,刚刚意识到我的构造函数是错误的。它现在似乎正在工作。谢谢。
        • 为什么将其读取为字符串,然后隐蔽到映射?可以直接读成地图
        猜你喜欢
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-23
        • 2018-09-11
        • 2019-11-06
        • 2020-12-09
        相关资源
        最近更新 更多