简体   繁体   English

RestEntity不正常返回空的正文

[英]RestEntity Not Working returns empty Body

I am using spring 2.0.5 Release as Parent. 我使用Spring 2.0.5 Release作为Parent。 I am new to Spring. 我是Spring的新手。 And I am using this API https://www.metaweather.com/api/location/2487956/ to try and display it using Object Mapper and I am having an issue with RestTemplate. 我正在使用此API https://www.metaweather.com/api/location/2487956/尝试使用对象映射器显示它,并且RestTemplate出现问题。

public void read() throws IOException {
     RestTemplate rest = new RestTemplate();
     ResponseEntity<String> response  = rest.getForEntity("https://www.metaweather.com/api/location/2487956", String.class);
     ObjectMapper mapper = new ObjectMapper();
     JsonNode root = mapper.readTree(response.getBody());
     JsonNode name = root.path("weather_state_name");

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

Your RestEntity is fine you're not parsing your response properly. 您的RestEntity很好,您没有正确解析您的响应。 The weather_state_name is in an array named consolidated_weather. weather_state_name位于名为solidated_weather的数组中。 Something following will work a/c to your needs. 以下内容将满足您的需求。

        ObjectMapper mapper = new ObjectMapper();
        JsonNode root = mapper.readTree(response.getBody());
        JsonNode consolidated_weather =root.get("consolidated_weather");
        consolidated_weather.forEach(x -> {

            System.out.println( x.get("weather_state_name"));
        });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM