无法使用JAXB解析XML响应 - IndexOutOfBoundsException

时间:2015-12-19 05:32:42

标签: java xml jaxb

我收到以下响应,并尝试使用JAXB解析它,但下面的代码返回IndexOutOfBoundsException。

我不确定代码有什么问题。我怀疑TotalChargeableReckratetotal,试图将它们作为单独的类但没有帮助。

EasytobookResponse response = restTemplate.postForObject(
                    url, easy, EasytobookResponse.class);

            System.err.println("RESPONSE >>>>"
                    + response.getResponse().getHotelInfo().get(0).getId());


@XmlRootElement(name = "EasytobookResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class EasytobookResponse {
    @XmlAttribute(name = "target")
    private String target;
    @XmlAttribute(name = "username")
    private String username;
    @XmlElement(name = "Response")
    private Response response;

    getters and setters 

@XmlRootElement(name = "Response")
@XmlAccessorType(XmlAccessType.FIELD)
public class Response {
    @XmlElementWrapper(name="Hotelinfo")
    @XmlElement(name="Hotelinfo")
    private List<Hotel> hotelInfo;

    getters and setters


@XmlRootElement(name = "Hotel")
@XmlAccessorType(XmlAccessType.FIELD)
public class Hotel {
    @XmlElement(name = "Id")
    private int id;
    @XmlElement(name = "FacilityGroups")
    private FacilityGroups facilityGroups;
    @XmlElement(name = "exact")
    private int exact;
    @XmlElementWrapper(name = "Roomtype")
    @XmlElement(name = "Roomtype")
    private List<Roomtype> roomType;

    getters and setters


@XmlRootElement(name = "FacilityGroups")
@XmlAccessorType(XmlAccessType.FIELD)
public class FacilityGroups {
    @XmlElement(name = "HandicapFacilities")
    private int handicapFacilities;
    @XmlElement(name = "HasInternet")
    private int hasInternet;
    @XmlElement(name = "HasParking")
    private int hasParking;
    @XmlElement(name = "PetsAllowed")
    private int petsAllowed;
    @XmlElement(name = "HasChildDiscount")
    private int hasChildDiscount;
    @XmlElement(name = "HasSwimmingPool")
    private int hasSwimmingPool;
    @XmlElement(name = "HasAirCon")
    private int hasAirCon;
    @XmlElement(name = "HasFitnessFacilities")
    private int hasFitnessFacilities;
    @XmlElement(name = "NonSmoking")
    private int nonSmoking;

    getters and setters

@XmlRootElement(name = "Roomtype")
@XmlAccessorType(XmlAccessType.FIELD)
public class Roomtype {
    @XmlAttribute(name = "parentid")
    private String parentid;
    @XmlElement(name = "Roomid")
    private long roomId;
    @XmlElement(name = "Roomname")
    private String roomName;
    @XmlElement(name = "Capacity")
    private int capacity;
    @XmlElement(name = "Available")
    private int available;

    @XmlElement(name = "Totalrate")
    private double totalRate;
    @XmlAttribute(name="currency")
    private String currency;
    @XmlAttribute(name="EUR")
    @XmlElement(name = "TotalTaxesAndFees")
    private double totalTaxesAndFees;
    @XmlElement(name = "TotalChargeable")
    private double totalChargeable;
    @XmlElement(name = "Reckratetotal")
    private double reckRateTotal;
    @XmlElement(name = "Breakfast")
    private int breakfast;
    @XmlElement(name = "Booklink")
    private String bookLink;
    @XmlElement(name = "Hoteldetailslink")
    private String hotelDetailsLink;
    @XmlElement(name = "Rtoken")
    private String rToken;
    @XmlElement(name = "Specialoffers")
    private String specialOffers;
    @XmlElement(name = "Bookingconditions")
    private BookingConditions bookingConditions;
    @XmlElement(name = "Rateinfo")
    private RateInfo rateInfo;

    getters and setters

@XmlRootElement(name = "Bookingconditions")
@XmlAccessorType(XmlAccessType.FIELD)
public class BookingConditions {
    @XmlElement(name = "Bookable")
    private int bookable;
    @XmlElement(name = "Chargepoint")
    private String chargePoint;
    @XmlElement(name = "Requirements")
    private Requirement requirement;

    getters and setters


@XmlRootElement(name = "Requirements")
@XmlAccessorType(XmlAccessType.FIELD)
public class Requirement {
    @XmlElement(name = "Capacity")
    private int capacity;
    @XmlElement(name = "Creditcardcvc")
    private int creditCardCvc;
    @XmlElement(name = "Billingaddress")
    private int billingAddress;

    getters and setters


@XmlRootElement(name = "Rateinfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class RateInfo {
    @XmlElement(name = "Underoccupancy")
    private int underoccupancy;
    @XmlElement(name = "Earlybooking")
    private int earlyBooking;
    @XmlElement(name = "Lastminutebooking")
    private int lastMinuteBooking;
    @XmlElement(name = "Nonrefundable")
    private int nonRefundable;
    @XmlElement(name = "Breakfastincluded")
    private int breakfastIncluded;

    getters and setters

响应

   <?xml version="1.0" encoding="UTF-8"?>
<EasytobookResponse>
    <Response target="test" username="project121">
        <Hotelinfo>
            <Hotel>
                <Id>436924</Id>
                <FacilityGroups>
                    <HandicapFacilities>0</HandicapFacilities>
                    <HasInternet>1</HasInternet>
                    <HasParking>1</HasParking>
                    <PetsAllowed>0</PetsAllowed>
                    <HasChildDiscount>0</HasChildDiscount>
                    <HasSwimmingPool>0</HasSwimmingPool>
                    <HasAirCon>1</HasAirCon>
                    <HasFitnessFacilities>0</HasFitnessFacilities>
                    <NonSmoking>1</NonSmoking>
                </FacilityGroups>
                <exact>436924</exact>
                <Roomtype>
                    <Roomid parentid="2069840686">2069840686</Roomid>
                    <Roomname>Two Persons Standard + Breakfast</Roomname>
                    <Capacity>2</Capacity>
                    <Available>8</Available>
                    <Totalrate currency="AUD" EUR="1204.3398">1800</Totalrate>
                    <TotalTaxesAndFees currency="AUD" EUR="0">0
                    </TotalTaxesAndFees>
                    <TotalChargeable currency="AUD" EUR="1204.3398">1800
                    </TotalChargeable>
                    <Reckratetotal currency="AUD" EUR="1204.3398">1800
                    </Reckratetotal>
                    <Breakfast>1</Breakfast>
                    <Booklink>https://stage-site.easytobook.us/booking_screen.php?hotel_id=436924&amp;exact=436924&amp;city_id=23&amp;arrival=20-11-2016&amp;departure=29-11-2016&amp;currency=EUR&amp;lang=en&amp;room[2069840686]=1&amp;persons=2&amp;rooms=1&amp;amu=280828207&amp;utm_source=project121&amp;utm_medium=affiliate&amp;utm_term=Sydney&amp;utm_content=etb5&amp;utm_campaign=en&amp;rtoken=1VSwnz7whjuxw-RGo5aP6cp-XmdDQSHC7twXmVrwPejOzYDFXdGY_bdbs9xbHuP8xU83qSmzJGz_vgQZHjeE5kVENJkuPBAtuKd6jTqIWCk,
                    </Booklink>
                    <Hoteldetailslink>http://stage-site.easytobook.us/hotel_proxy.php?hotel_id=436924&amp;lang=en&amp;arrival=20-11-2016&amp;departure=29-11-2016&amp;currency=EUR&amp;prs_arr%5B0%5D=2&amp;amu=280828207&amp;utm_source=project121&amp;utm_medium=affiliate&amp;utm_term=Sydney&amp;utm_content=etb5&amp;utm_campaign=en&amp;rtoken=1VSwnz7whjuxw-RGo5aP6cp-XmdDQSHC7twXmVrwPejOzYDFXdGY_bdbs9xbHuP8xU83qSmzJGz_vgQZHjeE5kVENJkuPBAtuKd6jTqIWCk,
                    </Hoteldetailslink>
                    <Rtoken>1VSwnz7whjuxw-RGo5aP6cp-XmdDQSHC7twXmVrwPejOzYDFXdGY_bdbs9xbHuP8xU83qSmzJGz_vgQZHjeE5kVENJkuPBAtuKd6jTqIWCk,
                    </Rtoken>
                    <Specialoffers />
                    <Bookingconditions>
                        <Bookable>1</Bookable>
                        <Chargepoint>hotel</Chargepoint>
                        <Requirements>
                            <Capacity>1</Capacity>
                            <Creditcardcvc>1</Creditcardcvc>
                            <Billingaddress>1</Billingaddress>
                        </Requirements>
                    </Bookingconditions>
                    <Rateinfo>
                        <Underoccupancy>0</Underoccupancy>
                        <Earlybooking>0</Earlybooking>
                        <Lastminutebooking>0</Lastminutebooking>
                        <Nonrefundable>0</Nonrefundable>
                        <Breakfastincluded>1</Breakfastincluded>
                    </Rateinfo>
                </Roomtype>
                <Roomtype>
                    <Roomid parentid="1379794752">1379794752</Roomid>
                    <Roomname>Two Persons Superior + Breakfast</Roomname>
                    <Capacity>2</Capacity>
                    <Available>4</Available>
                    <Totalrate currency="AUD" EUR="1264.5568">1890</Totalrate>
                    <TotalTaxesAndFees currency="AUD" EUR="0">0
                    </TotalTaxesAndFees>
                    <TotalChargeable currency="AUD" EUR="1264.5568">1890
                    </TotalChargeable>
                    <Reckratetotal currency="AUD" EUR="1264.5568">1890
                    </Reckratetotal>
                    <Breakfast>1</Breakfast>
                    <Booklink>https://stage-site.easytobook.us/booking_screen.php?hotel_id=436924&amp;exact=436924&amp;city_id=23&amp;arrival=20-11-2016&amp;departure=29-11-2016&amp;currency=EUR&amp;lang=en&amp;room[1379794752]=1&amp;persons=2&amp;rooms=1&amp;amu=280828207&amp;utm_source=project121&amp;utm_medium=affiliate&amp;utm_term=Sydney&amp;utm_content=etb5&amp;utm_campaign=en&amp;rtoken=ocGkvI7xuJJwz1BWWYNBztr7n-__tI8fVNfz3cZsrwRMGrtuHAEGziCH-0poK2ZoveEs-4Fz1_Y4U8pwE4KGKjJc4iwdSKM4ewIJwMU8omA,
                    </Booklink>
                    <Hoteldetailslink>http://stage-site.easytobook.us/hotel_proxy.php?hotel_id=436924&amp;lang=en&amp;arrival=20-11-2016&amp;departure=29-11-2016&amp;currency=EUR&amp;prs_arr%5B0%5D=2&amp;amu=280828207&amp;utm_source=project121&amp;utm_medium=affiliate&amp;utm_term=Sydney&amp;utm_content=etb5&amp;utm_campaign=en&amp;rtoken=ocGkvI7xuJJwz1BWWYNBztr7n-__tI8fVNfz3cZsrwRMGrtuHAEGziCH-0poK2ZoveEs-4Fz1_Y4U8pwE4KGKjJc4iwdSKM4ewIJwMU8omA,
                    </Hoteldetailslink>
                    <Rtoken>ocGkvI7xuJJwz1BWWYNBztr7n-__tI8fVNfz3cZsrwRMGrtuHAEGziCH-0poK2ZoveEs-4Fz1_Y4U8pwE4KGKjJc4iwdSKM4ewIJwMU8omA,
                    </Rtoken>
                    <Specialoffers />
                    <Bookingconditions>
                        <Bookable>1</Bookable>
                        <Chargepoint>hotel</Chargepoint>
                        <Requirements>
                            <Capacity>1</Capacity>
                            <Creditcardcvc>1</Creditcardcvc>
                            <Billingaddress>1</Billingaddress>
                        </Requirements>
                    </Bookingconditions>
                    <Rateinfo>
                        <Underoccupancy>0</Underoccupancy>
                        <Earlybooking>0</Earlybooking>
                        <Lastminutebooking>0</Lastminutebooking>
                        <Nonrefundable>0</Nonrefundable>
                        <Breakfastincluded>1</Breakfastincluded>
                    </Rateinfo>
                </Roomtype>
            </Hotel>
            .....

1 个答案:

答案 0 :(得分:2)

Response.hotelInfo@XmlElementWrapper(name="Hotelinfo")@XmlElement(name="Hotelinfo"),这意味着XML必须是:

<EasytobookResponse>
    <Response target="test" username="project121">
        <Hotelinfo>
            <Hotelinfo>
                <Id>436924</Id>

要匹配您的XML,@XmlElement应为Hotel,而不是Hotelinfo

目前,正在跳过<Hotel>元素,并且没有为Response.hotelInfo列表分配任何内容,因此response.getResponse().getHotelInfo().get(0)失败。

下次,请显示提供异常的行。使我们更容易帮助您。