簡體   English   中英

RestEntity不正常返回空的正文

[英]RestEntity Not Working returns empty Body

我使用Spring 2.0.5 Release作為Parent。 我是Spring的新手。 我正在使用此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());
}

您的RestEntity很好,您沒有正確解析您的響應。 weather_state_name位於名為solidated_weather的數組中。 以下內容將滿足您的需求。

        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