我是 Spring 新手,我正在使用 RestTemplete 将 Yahoo Weather API 的响应映射到 POJO 对象。我想要的只是“item”下的“yweather:forecast”,但由于某种原因它无法映射到我创建的类,“yweather:condition”也是如此,但我尝试了map“title”并且它起作用了,所以我很困惑我在哪里做错了。这是 XML 响应:
<rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" version="2.0">
<channel>
<title>Yahoo! Weather - Dallas, TX</title>
<link>...</link>
<description>Yahoo! Weather for Dallas, TX</description>
<language>en-us</language>
<lastBuildDate>Sun, 14 Dec 2014 8:52 am CST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Dallas" region="TX" country="US"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="62" direction="160" speed="14"/>
<yweather:atmosphere humidity="90" visibility="4" pressure="29.99" rising="1"/>
<yweather:astronomy sunrise="7:20 am" sunset="5:21 pm"/>
<image>...</image>
<item>
<title>Conditions for Dallas, TX at 8:52 am CST</title>
<geo:lat>32.85</geo:lat>
<geo:long>-96.85</geo:long>
<link>...</link>
<pubDate>Sun, 14 Dec 2014 8:52 am CST</pubDate>
<yweather:condition text="Cloudy" code="26" temp="62" date="Sun, 14 Dec 2014 8:52 am CST"/>
<description>...</description>
<yweather:forecast day="Sun" date="14 Dec 2014" low="53" high="69" text="PM Thunderstorms" code="38"/>
<yweather:forecast day="Mon" date="15 Dec 2014" low="42" high="66" text="Sunny" code="32"/>
<yweather:forecast day="Tue" date="16 Dec 2014" low="41" high="57" text="Sunny" code="32"/>
<yweather:forecast day="Wed" date="17 Dec 2014" low="46" high="51" text="PM Light Rain" code="11"/>
<yweather:forecast day="Thu" date="18 Dec 2014" low="47" high="54" text="Light Rain" code="11"/>
<guid isPermaLink="false">USTX0327_2014_12_18_7_00_CST</guid>
</item>
</channel>
</rss>
RSS类:
@XmlRootElement(name = "rss")
@XmlAccessorType(XmlAccessType.FIELD)
public class Rss {
@XmlElement(name = "channel")
private Channel channel;
}
频道类:
@XmlAccessorType(XmlAccessType.FIELD)
public class Channel {
@XmlElement(name="item")
private Item item;
}
物品类别:
@XmlAccessorType(XmlAccessType.FIELD)
public class Item {
@XmlElement(name="yweather:forecast", type = Forecast.class)
private List<Forecast> forecast;
//Don't really need, just for testing
@XmlElement(name = "yweather:condition", type = Condition.class)
private Condition condition;
//Don't really need, just for testing
@XmlElement(name = "title")
private String title;
}
预测类:
@XmlAccessorType(XmlAccessType.FIELD)
public class Forecast {
@XmlAttribute(name="day")
private String day;
@XmlAttribute(name="date")
private String date;
@XmlAttribute(name="low")
private int low;
@XmlAttribute(name="high")
private int high;
@XmlAttribute(name="text")
private String text;
@XmlAttribute(name="code")
private int code;
}
司机:
public class Application {
public static void main(String args[]) {
RestTemplate restTemplate = new RestTemplate();
Rss rss = restTemplate.getForObject("http://xml.weather.yahoo.com/forecastrss?p=75248", Rss.class);
System.out.println(rss);
}
}
这是调试期间的对象树:
标题已映射,但“条件”和“预测”都没有,我不确定我做错了什么。
这是我第一次使用restTemplete,所以我只是按照人们在互联网上的做法。我猜它可能与元素“yweather:forecast”有关?
谢谢!