【问题标题】:Spring RestTemplete - Could not map XML to ObjectSpring RestTemplate - 无法将 XML 映射到对象
【发布时间】:2014-12-14 18:53:10
【问题描述】:

我是 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”元素有关?

谢谢!

【问题讨论】:

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


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-16
    • 2019-07-22
    • 1970-01-01
    • 2021-04-13
    • 2015-05-07
    • 2016-11-25
    • 1970-01-01
    • 2017-01-19
    相关资源
    最近更新 更多