Spring RestTemplate - 无法将XML映射到Object

时间:2014-12-14 18:53:11

标签: xml spring jaxb resttemplate yahoo-weather-api

我是Spring的新手,我使用RestTemplete将Yahoo Weather API的响应映射到POJO对象。我想要的只是" yweather:预测"在" item"下,但由于某种原因,它无法映射到我创建的类,所以" 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);
    }
}

以下是调试期间的对象树:

enter image description here

标题已被映射,但是#34;条件&#34;也不是预测&#34;,我不确定我在哪里做错了。

这是我第一次使用restTemplete,所以我只关注人们从互联网上做的事情。我猜测它可能与元素有关&#34; yweather:forecast&#34;?

谢谢!

1 个答案:

答案 0 :(得分:0)

感谢@Blaise Doughan,我在他的链接中找到了解决方案:

http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

问题是我没有指定名称空间,我认为这是元素名称的一部分。