簡體   English   中英

無法使用JAXB解析XML響應-IndexOutOfBoundsException

[英]Cannot parse XML response with JAXB - IndexOutOfBoundsException

我收到以下響應,並嘗試使用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>
            .....

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)失敗。


下次,請顯示出現異常的行。 讓我們輕松地為您提供幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM