Spring RestTemplete - не удалось сопоставить XML с объектом

Я новичок в Spring и использую RestTemplete для сопоставления ответа от Yahoo Weather API с объектами POJO. Все, что мне нужно, это «yweather: прогноз» в разделе «item», но по какой-то причине его нельзя было сопоставить с созданным мной классом, как и «yweather: condition», но я попробовал сопоставить «название», и это сработало, так что Я смущен тем, где я сделал неправильно. Вот 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: прогноз»?

Спасибо!


person YueQi Li    schedule 14.12.2014    source источник
comment
Следующее поможет: blog.bdoughan.com/2010/08/jaxb-namespaces. .html   -  person bdoughan    schedule 14.12.2014
comment
@BlaiseDoughan Это сработало! Спасибо! Можете ли вы опубликовать ответ, чтобы я мог выбрать?   -  person YueQi Li    schedule 14.12.2014


Ответы (1)


Благодаря @Blaise Doughan я нашел решение по его ссылке:

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

Проблема заключалась в том, что я не указал пространство имен, которое, как мне казалось, является частью имени элемента.

person YueQi Li    schedule 26.03.2015