代码能够解析除一个以外的所有对象

时间:2016-06-19 04:35:36

标签: java xml spring jaxb resttemplate

我能够从服务器接收响应,但无法访问响应的一部分。不确定如何解决问题。我将RrateInfos更改为数组,但它没有帮助,还检查了所有与我的响应相符的数据类型。

这不是重复,问题不在于nullPointerException它与Web服务和解析响应有关。

换句话说,我知道可以声明ChargeableRateInfo不接收NullPointerExcpetion但问题是当我从服务器收到响应时我需要填充它!

任何帮助都将不胜感激。

响应

<ns2:HotelListResponse xmlns:ns2="http://v3.hotel.wsapi.ean.com/">
<customerSessionId>XXXXXX-XXXXXX-XXXXX-XXXXX-XXXXXX</customerSessionId>
<numberOfRoomsRequested>1</numberOfRoomsRequested>
<moreResultsAvailable>true</moreResultsAvailable>
<cacheKey>5byu3434:13434328d0ed:-22319</cacheKey>
<cacheLocation>1.1.1.1:7999</cacheLocation>
<cachedSupplierResponse supplierCacheTolerance="NOT_SUPPORTED" cachedTime="0" supplierRequestNum="1111" supplierResponseNum="11" supplierResponseTime="1111" candidatePreptime="45" otherOverheadTime="9" tpidUsed="6666" matchedCurrency="true" matchedLocale="true"/>
<HotelList size="20" activePropertyCount="1375">
<HotelSummary order="0" ubsScore="1024995">
<hotelId>3345698</hotelId>
<name>Park Plaza Westminster Bridge London</name>
<address1>200 Westminster Bridge Road</address1>
<city>London</city>
<postalCode>SE1 7UT</postalCode>
<countryCode>GB</countryCode>
<airportCode>LCY</airportCode>
<supplierType>E</supplierType>
<propertyCategory>1</propertyCategory>
<hotelRating>4.0</hotelRating>
<confidenceRating>45</confidenceRating>
<amenityMask>1442315</amenityMask>
<tripAdvisorRating>4.5</tripAdvisorRating>
<tripAdvisorReviewCount>11972</tripAdvisorReviewCount>
<tripAdvisorRatingUrl>...</tripAdvisorRatingUrl>
<locationDescription>Near London Aquarium</locationDescription>
<shortDescription>...</shortDescription>
<highRate>300.10</highRate>
<lowRate>200.02</lowRate>
<rateCurrencyCode>USD</rateCurrencyCode>
<latitude>51.50111</latitude>
<longitude>-0.11733</longitude>
<proximityDistance>0.7890795</proximityDistance>
<proximityUnit>MI</proximityUnit>
<hotelInDestination>true</hotelInDestination>
<thumbNailUrl>...</thumbNailUrl>
<deepLink>...</deepLink>
<RoomRateDetailsList>
<RoomRateDetails>
<roomTypeCode>200019300</roomTypeCode>
<rateCode>205428827</rateCode>
<maxRoomOccupancy>2</maxRoomOccupancy>
<quotedRoomOccupancy>1</quotedRoomOccupancy>
<minGuestAge>0</minGuestAge>
<roomDescription>Superior Room, 1 King Bed, Accessible</roomDescription>
<propertyAvailable>true</propertyAvailable>
<propertyRestricted>false</propertyRestricted>
<expediaPropertyId>3113039</expediaPropertyId>
<RateInfos size="1">
<RateInfo priceBreakdown="true" promo="true" rateChange="true">
<RoomGroup>
<Room>
<numberOfAdults>1</numberOfAdults>
<numberOfChildren>0</numberOfChildren>
<rateKey>59e77b78-044f-45e1-84c4-223f2d090518-5001</rateKey>
</Room>
</RoomGroup>
<ChargeableRateInfo averageBaseRate="257.48" averageRate="170.03" commissionableUsdTotal="1020.20" currencyCode="USD" maxNightlyRate="175.06" nightlyRateTotal="850.16" surchargeTotal="170.04" total="1020.20">
<NightlyRatesPerRoom size="5">
<NightlyRate baseRate="252.40" rate="166.68" promo="true"/>
<NightlyRate baseRate="265.09" rate="175.06" promo="true"/>
<NightlyRate baseRate="265.09" rate="175.06" promo="true"/>
<NightlyRate baseRate="252.40" rate="166.68" promo="true"/>
<NightlyRate baseRate="252.40" rate="166.68" promo="true"/>
</NightlyRatesPerRoom>
<Surcharges size="1">
<Surcharge type="TaxAndServiceFee" amount="170.04"/>
</Surcharges>
</ChargeableRateInfo>
<nonRefundable>true</nonRefundable>
<rateType>MerchantStandard</rateType>
<promoId>218622661</promoId>
<promoDescription>Save 30%</promoDescription>
<promoType>Standard</promoType>
<currentAllotment>6</currentAllotment>
</RateInfo>
</RateInfos>
<ValueAdds size="1">
<ValueAdd id="2048">
<description>Free Wireless Internet</description>
</ValueAdd>
</ValueAdds>
</RoomRateDetails>
</RoomRateDetailsList>
</HotelSummary>
<HotelSummary order="1" ubsScore="1020278">
......

@XmlRootElement(name = "HotelListResponse")
@XmlAccessorType(XmlAccessType.FIELD)
public class HotelListResponse {
    @XmlElement(name = "customerSessionId")
    private String customerSessionId;
    @XmlElement(name = "numberOfRoomsRequested")
    private short numberOfRoomsRequested;
    @XmlElement(name = "moreResultsAvailable")
    private boolean moreResultsAvailable;
    @XmlElement(name = "cacheKey")
    private String cacheKey;
    @XmlElement(name = "cacheLocation")
    private String cacheLocation;
    @XmlElement(name = "HotelList")
    private HotelList hotelList;

HotelList

@XmlRootElement(name = "HotelList")
@XmlAccessorType(XmlAccessType.FIELD)
public class HotelList {
    @XmlAttribute(name = "size")
    private int size;
    @XmlAttribute(name = "activePropertyCount")
    private int activePropertyCount;
    @XmlElement(name = "HotelSummary")
    private List<HotelSummary> hotelSummaries;

HotelSummary

@XmlRootElement(name = "HotelSummary")
@XmlAccessorType(XmlAccessType.FIELD)
public class HotelSummary {
    @XmlAttribute(name = "order")
    private int order;
    @XmlAttribute(name = "ubsScore")
    private long ubsScore;
    @XmlElement(name = "hotelId")
    private int hotelId;
    @XmlElement(name = "name")
    private String name;
    @XmlElement(name = "address1")
    private String address1;
    @XmlElement(name = "city")
    private String city;
    @XmlElement(name = "postalCode")
    private int postalCode;
    @XmlElement(name = "countryCode")
    private String countryCode;
    @XmlElement(name = "airportCode")
    private String airportCode;
    @XmlElement(name = "supplierType")
    private String supplierType;
    @XmlElement(name = "propertyCategory")
    private int propertyCategory;
    @XmlElement(name = "hotelRating")
    private float hotelRating;
    @XmlElement(name = "confidenceRating")
    private int confidenceRating;
    @XmlElement(name = "amenityMask")
    private int amenityMask;
    @XmlElement(name = "tripAdvisorRating")
    private double tripAdvisorRating;
    @XmlElement(name = "tripAdvisorReviewRating")
    private double tripAdvisorReviewRating;
    @XmlElement(name = "tripAdvisorRatingUrl")
    private String tripAdvisorRatingUrl;
    @XmlElement(name = "locationDescription")
    private String locationDescription;
    @XmlElement(name = "shortDescription")
    private String shortDescription;
    @XmlElement(name = "highRate")
    private double highRate;
    @XmlElement(name = "lowRate")
    private double lowRate;
    @XmlElement(name = "rateCurrencyCode")
    private String rateCurrencyCode;
    @XmlElement(name = "latitude")
    private double latitude;
    @XmlElement(name = "longitude")
    private double longitude;
    @XmlElement(name = "proximityDistance")
    private double proximityDistance;
    @XmlElement(name = "proximityUnit")
    private String proximityUnit;
    @XmlElement(name = "hotelInDestination")
    private boolean hotelInDestination;
    @XmlElement(name = "thumbNailUrl")
    private String thumbNailUrl;
    @XmlElement(name = "deepLink")
    private String deepLink;
    @XmlElement(name = "RoomRateDetailsList")
    private RoomRateDetailsList roomRateDetailsList;

RoomRateDetailsList

@XmlRootElement(name = "RoomRateDetailsList")
@XmlAccessorType(XmlAccessType.FIELD)
public class RoomRateDetailsList {
@XmlElement(name = "RoomRateDetails")
private RoomRateDetails roomRateDetails;

RoomRateDetails

@XmlRootElement(name = "RoomRateDetails")
@XmlAccessorType(XmlAccessType.FIELD)
public class RoomRateDetails {
@XmlElement(name = "roomTypeCode")
private int roomTypeCode;
@XmlElement(name = "rateCode")
private int rateCode;
@XmlElement(name = "maxRoomOccupancy")
private int maxRoomOccupancy;
@XmlElement(name = "quotedRoomOccupancy")
private int quotedRoomOccupancy;
@XmlElement(name = "minGuestAge")
private int minGuestAge;
@XmlElement(name = "roomDescription")
private String roomDescription;
@XmlElement(name = "propertyAvailable")
private boolean propertyAvailable;
@XmlElement(name = "propertyRestricted")
private boolean propertyRestricted;
@XmlElement(name = "expediaPropertyId")
private int expediaPropertyId;
@XmlElement(name="RateInfos")
private RateInfo rateInfos;

RoomRate

@XmlRootElement(name = "RateInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class RateInfo {
    @XmlAttribute(name = "priceBreakdown")
    private boolean priceBreakdown;
    @XmlAttribute(name = "rateChange")
    private boolean rateChange;
    @XmlAttribute(name = "promo")
    private boolean promo;
    @XmlElement(name = "RoomGroup")
    private RoomGroup roomGroup;
    @XmlElement(name = "ChargeableRateInfo")
    private ChargeableRateInfo chargeableRateInfo;
    @XmlElement(name = "nonRefunable")
    private boolean nonRefundable;
    @XmlElement(name = "rateType")
    private String rateType;
    @XmlElement(name = "promoId")
    private long promoId;
    @XmlElement(name = "promoDescription")
    private String promoDescription;
    @XmlElement(name = "currentAllotment")
    private int currentAllotment;

ChargeableRateInfo

@XmlRootElement(name = "ChargeableRateInfo")
@XmlAccessorType(XmlAccessType.FIELD)
public class ChargeableRateInfo {
    @XmlAttribute(name = "averageBaseRate")
    private double averageBaseRate;
    @XmlAttribute(name = "averageRate")
    private double averageRate;
    @XmlAttribute(name = "commissionableUsdTotal")
    private double commissionableUsdTotal;
    @XmlAttribute(name = "currencyCode")
    private String currencyCode;
    @XmlAttribute(name = "maxNightlyRate")
    private double maxNightlyRate;
    @XmlAttribute(name = "nightlyRateTotal")
    private double nightlyRateTotal;
    @XmlAttribute(name = "surchargeTotal")
    private double surchargeTotal;
    @XmlAttribute(name = "total")
    private double total;
    @XmlElement(name = "NightlyRatesPerRoom")
    private NightlyRatesPerRoom nightlyRatesPerRoom;
    @XmlElement(name = "Surcharges")
    private Surcharges surcharges;

附加费

@XmlRootElement(name = "Surcharges")
@XmlAccessorType(XmlAccessType.FIELD)
public class Surcharges {
    @XmlAttribute(name = "size")
    private int size;
    @XmlElement(name = "Surcharge")
    private List<Surcharge> surcharges;

附加费

@XmlRootElement(name = "Surcharge")
@XmlAccessorType(XmlAccessType.FIELD)
public class Surcharge {
    @XmlAttribute(name = "type")
    private String type;
    @XmlAttribute(name = "amount")
    private double amount;

代码

    RestTemplate restTemplate = new RestTemplate();
    HotelListResponse hotelResponse = restTemplate.getForObject(URL,
            HotelListResponse.class);
    System.err.println("get number of rooms requested>>>"
            + hotelResponse.getNumberOfRoomsRequested());
    System.err.println(" Hotel Summariesssssssss Size>>>"
            + hotelResponse.getHotelList().getHotelSummaries().size());
    for (HotelSummary hotel : hotelResponse.getHotelList()
            .getHotelSummaries()) {
        System.err.println("Hotel Name >>" + hotel.getName());
        System.err.println("RoomRateDetails toString:"
                + hotel.getRoomRateDetailsList().getRoomRateDetails()
                        .getRateInfos().getChargeableRateInfo() != null);
        System.err.println("to total:"
                + hotel.getRoomRateDetailsList().getRoomRateDetails()
                        .getRateInfos().getChargeableRateInfo().getTotal()); 
        //error is ^^^^^^^^^^^^^ line 91

控制台

get number of rooms requested>>>1  
Hotel Summariesssssssss Size>>>20 
Hotel Name >>Radisson Blu Portman Hotel 
true 
Jun 19, 2016 2:29:48 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [pr] in context with path [/Project1] 
threw exception [Request processing failed; nested exception is 
java.lang.NullPointerException] with root cause java.lang.NullPointerException  
at com.myproject.services.HotelsServiceImpl.eoa(HotelsServiceImpl.java:91)

0 个答案:

没有答案