无法使用JAXB解析xml响应 - 抛出NullPointerException

时间:2016-07-18 08:04:05

标签: java xml spring-mvc jaxb resttemplate

我正在尝试解析Web服务的响应,但代码抛出 '的NullPointerException&#39 ;.我向Restaurant类添加了XmlRootElement注释,但抛出了同样的异常。

回复类似于以下

<RestaurantList>
 <Restaurant id="100001">
     <Url>URL is here</Url>
     <Name>Agaton</Name>
     <Description>
       <Text> Text is here</Text>
     </Description>
     <Image>
       <Code>111772</Code>
       <Name>MAINCOURSE</Name>
       <Description><Text>Main course Agaton</Text></Description>
       <Url>http://img.static-
                     bookatable.com/17862a2d1fa13f5fb083a4d78d6f7e42.jpg</Url>
     </Image>
     <Image>
      <Code>111773</Code>
      <Name>MAINCOURSE</Name>
      <Url>http://img.static-
          bookatable.com/8fae3334490c276322ac1a597fc2d456.jpg</Url>
     </Image>
     <PrimaryCuisine>Italian</PrimaryCuisine>   
     <Address>
       <Line1>Västerlånggatan 72</Line1>
       <Line2/>
       <Line3/>
       <PostalCode>111 29</PostalCode>
       <City>Stockholm</City>
       <Country>SWE</Country>
   </Address>
   <Geo>
      <Latitude>59.32299</Latitude>
      <Longtitude>18.07205</Longtitude>
   </Geo>
   <Region></Region>
   <Detail>
      <Homepage>http://www.restaurangagaton.se</Homepage>   
      <EMail>kontakt@restaurangagaton.se</EMail>
      <PhoneNumber type="Main">+46 8207299</PhoneNumber>
      <PhoneNumber type="Fax">+46 8207107</PhoneNumber>
      <Styles>
       <Style>
         <Text>Neighbourhood Restaurant</Text>
       </Style>
      </Styles>
      <OpenHours>Sunday-Thursday 11.00-22.00 Friday-Saturday 11.00 
       23.00</OpenHours>
      <AveragePriceThreeCourseMeal 
         currency="SEK">550</AveragePriceThreeCourseMeal>
      <AveragePriceMainCourse currency="SEK">223</AveragePriceMainCourse>
   </Detail>
  </Restaurant>
  <Restaurant id="100005"><Url>

代码

        RestTemplate restTemplate = new RestTemplate();
        RestaurantList response = restTemplate.getForObject(url, RestaurantList.class);
        System.err.println("size>>" + response.getRestaurants().size()); //Exception

我有所有吸气剂和制定者

RestaurnatList

@XmlRootElement(name = "RestaurantList")
@XmlAccessorType(XmlAccessType.FIELD)
public class RestaurantList {
    @XmlElement(name = "restaurant")
    private List<Restaurant> restaurants;

餐厅

@XmlAccessorType(XmlAccessType.FIELD)
public class Restaurant{
    @XmlAttribute
    private long id;
    @XmlElement
    private String url;
    @XmlElement
    private String name;
    @XmlElement
    private Description description;
    @XmlElement
    private List<Image> image;
    @XmlElement
    private String primaryCuisine;
    @XmlElement(name = "address")
    private Address address;
    @XmlElement
    private Geo geo;
    @XmlElement
    private Region region;
    @XmlElement
    private Detail detail;

描述

@XmlAccessorType(XmlAccessType.FIELD)
public class Description {
    @XmlElement(name="text")
    private String text;

图片

@XmlAccessorType(XmlAccessType.FIELD)
public class Image {
    @XmlElement
    private long code;
    @XmlElement
    private String name;
    @XmlElement
    private Description description;
    @XmlElement
    private String url;

地址

@XmlAccessorType(XmlAccessType.FIELD)
public class Address {
    @XmlElement
    private String line1;
    @XmlElement
    private String line2;
    @XmlElement
    private String line3;
    @XmlElement
    private String postalCode;
    @XmlElement
    private String city;
    @XmlElement
    private String country;

地理

@XmlAccessorType(XmlAccessType.FIELD)
public class Geo {
    @XmlElement
    private double latitude;
    @XmlElement
    private double longtitude;

区域

@XmlAccessorType(XmlAccessType.FIELD)
public class Region {
    @XmlElement
    private long code;

详细

@XmlAccessorType(XmlAccessType.FIELD)
public class Detail {
    @XmlElement
    private String homepage;
    @XmlElement
    private String email;
    @XmlElement
    private String phoneNumber1;
    @XmlElement
    private String phoneNumber2;
    @XmlElement
    private Styles style;
    @XmlElement
    private String openHours;
    @XmlElement
    private double averagePriceThreeCourseMeal;
    @XmlElement
    private double averagePriceMainCourseMeal;

1 个答案:

答案 0 :(得分:0)

尝试在@XmlElement(name = "restaurant")课程中使用@XmlElement(name = "Restaurant")更改RestaurantList。它应该工作