我正在尝试发送REST请求,响应的第一部分可用,但我无法访问第二部分。
<SearchResults>
<TotalHotelsFound>7250</TotalHotelsFound>
<CheckInDate>2016-01-07</CheckInDate>
<CheckOutDate>2016-01-09</CheckOutDate>
<Currency>EUR</Currency>
<SearchId>HB-82365485</SearchId>
<Hotels>
<Hotel>
<HotelCode>FRYX72</HotelCode>
<ProcessId>O3-69817815</ProcessId>
<BoardType>Room Only</BoardType>
<BookingURL>http://www.otel.com/hotels/nomad_paris_roissy_cdg_hotel.htm?processid=HB-82326485FRYX734</BookingURL>
<TotalPrice>87</TotalPrice>
<FreeCancel>unknown</FreeCancel>
<PaymentType>prepaid</PaymentType>
<Tax>14</Tax>
<AverageNightRate>43</AverageNightRate>
<RoomTypes>Double Or Twin/Double Standard</RoomTypes>
<AverageRatePerNightPerRoom>43.36</AverageRatePerNightPerRoom>
<Rooms>
<Room>
<Type>double or twin standard</Type>
<RoomDesc>Double Or Twin Standard</RoomDesc>
<Adults>2</Adults>
<Children/>
<TotalRate>87</TotalRate>
<Tax>12</Tax>
<Rates/>
<AverageNightRoomRate>43</AverageNightRoomRate>
</Room>
</Rooms>
</Hotel>
<Hotel>
<HotelCode>FRYX72</HotelCode>
<ProcessId>O0-63816095</ProcessId>
<BoardType>Room Only</BoardType>
<BookingURL>http://www.otel.com/hotels/nomad_paris_roissy_cdg_hotel.htm?processid=HB-82356285FRYX789</BookingURL>
<TotalPrice>87</TotalPrice>
<FreeCancel>unknown</FreeCancel>
<PaymentType>prepaid</PaymentType>
<Tax>14</Tax>
<AverageNightRate>43</AverageNightRate>
<RoomTypes>Double Or Twin/Double Standard</RoomTypes>
<AverageRatePerNightPerRoom>43.36</AverageRatePerNightPerRoom>
<Rooms>
<Room>
<Type>double or twin standard</Type>
<RoomDesc>Double Or Twin Standard</RoomDesc>
<Adults>2</Adults>
<Children/>
<TotalRate>87</TotalRate>
<Tax>12</Tax>
<Rates/>
<AverageNightRoomRate>43</AverageNightRoomRate>
</Room>
</Rooms>
</Hotel>
代码
try {
System.err.println(">>> Otel");
final String AFFILIATE = "Username";
final String URL = "http://ws.hotelspro.com/xml/test-search-otel.php?affiliate="
+ AFFILIATE;
String readyUrl = URL + "&country=FR&city=Paris&checkin=2016-1-7&checkout=2016-1-9¤cy=EUR&rooms=1&adults1=2";
System.err.println(">>>" + readyUrl);
RestTemplate restTemplate = new RestTemplate();
SearchResults searchResults = restTemplate.getForObject(readyUrl,
SearchResults.class);
if (searchResults == null) {
System.err.println("it is null");
} else {
System.err.println("message:>>"
+ searchResults.getTotalHotelsFound());
}
System.err.println(">>>" + searchResults.getHotels().size());
System.err.println(">>>" + searchResults.getHotels().get(0).getHotelCode());
System.err.println(">>>" + searchResults.getHotels().get(0).getHotelCode());
System.err
.println("Otel>>>" + searchResults.getHotels().get(0).getPaymentType());
System.err.println("event>>" + searchResults.getHotels().get(0).getBookingUrl());
} catch (NullPointerException e) {
e.printStackTrace();
}
JAXB
@XmlRootElement(name = "SearchResults")
@XmlAccessorType(XmlAccessType.FIELD)
public class SearchResults {
@XmlElement(name = "TotalHotelsFound")
private int totalHotelsFound;
@XmlElement(name = "CheckInDate")
private Date checkInDate;
@XmlElement(name = "CheckOutDate")
private Date checkOutDate;
@XmlElement(name = "Currency")
private String currency;
@XmlElement(name = "SearchId")
private String searchId;
@XmlElement(name = "Hotels")
private List<Hotel> hotels;
getters and setters
@XmlRootElement(name = "Hotel")
@XmlAccessorType(XmlAccessType.FIELD)
public class Hotel {
@XmlElement(name = "HotelCode")
private String hotelCode;
@XmlElement(name = "ProcessId")
private String processId;
@XmlElement(name = "BoardType")
private String boardType;
@XmlElement(name = "BookingURL")
private String bookingUrl;
@XmlElement(name = "TotalPrice")
private double totalPrice;
@XmlElement(name = "FreeCancel")
private String freeCancel;
@XmlElement(name = "PaymentType")
private String paymentType;
@XmlElement(name = "Tax")
private int tax;
@XmlElement(name = "AverageNightRate")
private double averageNightRate;
@XmlElement(name = "RoomTypes")
private String roomType;
@XmlElement(name = "AverageRatePerNightPerRoom")
private double averageRatePerNightPerRoom;
@XmlElement(name = "Rooms")
private List<Room> rooms;
getters and setters
@XmlRootElement(name = "Room")
@XmlAccessorType(XmlAccessType.FIELD)
public class Room {
@XmlElement(name = "Type")
private String type;
@XmlElement(name = "RoomDesc")
private String roomDesc;
@XmlElement(name = "Adults")
private int adults;
@XmlElement(name = "Children")
private int children;
@XmlElement(name = "TotalRate")
private double totalRate;
@XmlElement(name = "Tax")
private double tax;
@XmlElement(name = "Rates")
private String rates;
@XmlElement(name = "AverageNightRoomRate")
private int averageNightRoomRate;
getters and setters
输出
我收到以下输出,如下图所示,第一部分已阅读,但酒店列表不会被填充。
message:>>7258
>>>1
>>>null
>>>null
Otel>>>null
event>>null
答案 0 :(得分:1)
您需要将@XmlElementWrapper用于列表
@XmlRootElement(name = "SearchResults")
@XmlAccessorType(XmlAccessType.FIELD)
public static class SearchResults {
@XmlElement(name = "TotalHotelsFound")
private int totalHotelsFound;
@XmlElement(name = "CheckInDate")
private Date checkInDate;
@XmlElement(name = "CheckOutDate")
private Date checkOutDate;
@XmlElement(name = "Currency")
private String currency;
@XmlElement(name = "SearchId")
private String searchId;
@XmlElementWrapper(name = "Hotels")
@XmlElement(name = "Hotel")
private List<Hotel> hotels;
}
和房间一样
答案 1 :(得分:0)
我相信您的示例XML中可能会有一些缺少的标记,特别是</Hotels>
和</SearchResults>
上面的代码表明您已尝试指定多个根元素。首先,这是不正确的。 SearchResults将是您的根元素,并且包含所有其他元素。这是更准确的JAXB代码:
SearchResults类:
@XmlRootElement(name = "SearchResults")
@XmlAccessorType(XmlAccessType.FIELD)
public class SearchResults {
@XmlElement(name = "TotalHotelsFound")
private int totalHotelsFound;
@XmlElement(name = "CheckInDate")
private Date checkInDate;
@XmlElement(name = "CheckOutDate")
private Date checkOutDate;
@XmlElement(name = "Currency")
private String currency;
@XmlElement(name = "SearchId")
private String searchId;
@XmlElement(name = "Hotels")
private Hotels hotels;
//getters and setters
}
酒店课程:
@XmlAccessorType(XmlAccessType.FIELD)
public class Hotels {
@XmlElement(name = "Hotel")
private List<Hotel> hotel;
//getters and setters
}
酒店课程:
@XmlAccessorType(XmlAccessType.FIELD)
public class Hotel {
@XmlElement(name = "HotelCode")
private String hotelCode;
@XmlElement(name = "ProcessId")
private String processId;
...etc
//getters and setters
}
我希望这会有所帮助。