I am using jackson-dataformat-xml.
I have the following classes:
public class CTHotel {
#JacksonXmlProperty(localName = "basic-info")
private HotelBaseInfo hotelBaseInfo;
//other properties and getters and setters
}
public class HotelBaseInfo {
#JacksonXmlProperty(localName = "hotel-name")
private String hotelName;
#JacksonXmlElementWrapper(localName = "hotel-amenities")
private List<HotelAmenity> hotelAmenities;
//other properties and getters/setters
}
public class HotelAmenity {
private String category;
private String description;
#JacksonXmlElementWrapper(localName = "amenities")
private List<String> amenities;
//other properties and getters/setters
}
My XML is this:
<hotels>
<hotel>
<basic-info>
<hotel-name>Hotel XYZ</hotel-name>
<hotel-amenities>
<hotel-amenity>
<category>F&B</category>
<description>Random Text</description>
<amenities>
<amenity>Cafe</amenity>
<amenity>Bar</amenity>
<amenity>Rastaurant</amenity>
</amenities>
</hotel-amenity>
<hotel-amenity>
...
</hotel-amenity>
</hotel-amenities>
</basic-info>
</hotel>
<hotel>
...
</hotel>
</hotels>
My question is, how can I map amenities as list of strings in my HotelAmenity class as mentioned above ? What annotation should I use on amenities field ?
#JacksonXmlElementWrapper annotation on hotelAmenities field of Hotel class is working just fine.
I get the below error while mapping :
com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_OBJECT token
at [Source: java.io.StringReader#507bcc81; line: 3, column: 1039] (through reference chain: com.example.response.HotelSearchResponse["hotels"]->java.util.ArrayList[2]->com.example.response.CTHotel["basic-info"]->com.example.response.HotelBaseInfo["hotel-amenities"]->java.util.ArrayList[1]->com.example.response.HotelAmenity["amenities"]->java.util.ArrayList[9])
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:148) ~[jackson-databind-2.6.5.jar:2.6.5]
at com.fasterxml.jackson.databind.DeserializationContext.mappingException(DeserializationContext.java:857) ~[jackson-databind-2.6.5.jar:2.6.5]
...
Here's the code, that I hope would answer your question:
/**Class Hotels*/
#JacksonXmlRootElement(localName = "hotels")
public class Hotels {
#JacksonXmlElementWrapper(useWrapping = false)
private List<Hotel> hotel;
//Other getters and setters
}
/**Class Hotel*/
#JacksonXmlRootElement(localName = "hotel")
public class Hotel {
#JacksonXmlProperty(localName = "basic-info")
private HotelBaseInfo hotelBaseInfo;
//Other getters and setters
}
/**Class HotelBaseInfo*/
public class HotelBaseInfo {
#JacksonXmlProperty(localName = "hotel-name")
private String hotelName;
#JacksonXmlElementWrapper(localName = "hotel-amenities")
private List<HotelAmenity> hotelAmenities;
//Other getters and setters
}
/**Class HotelAmenity*/
public class HotelAmenity {
private String category;
private String description;
#JacksonXmlElementWrapper(localName = "amenities")
private List<Amenities> amenity;
static class Amenities {
#JacksonXmlText
private String value;
}
//Other getters and setters
}
Here's what worked for me:
public class JacksonXmlParsing {
#JacksonXmlRootElement(localName = "hotels")
static class HotelSearchResponse {
#JacksonXmlElementWrapper(localName = "hotel")
private List<CTHotel> hotels;
//other properties and getters and setters
}
static class CTHotel {
#JacksonXmlProperty(localName = "hotel-name")
private String hotelName;
#JacksonXmlElementWrapper(localName = "hotel-amenities")
private List<HotelAmenity> hotelAmenities;
//other properties and getters and setters
}
static class HotelAmenity {
private String category;
private String description;
#JacksonXmlElementWrapper
private List<String> amenities;
//other properties and getters/setters
}
public static void main(String[] args) throws IOException {
XmlMapper xmlMapper = new XmlMapper();
File file = new File("./src/main/resources/file.xml");
HotelSearchResponse response = xmlMapper.readValue(file, HotelSearchResponse.class);
System.out.println(response);
}
}
Output:
HotelSearchResponse(hotels=[CTHotel(hotelName=Hotel XYZ, hotelAmenities=[HotelAmenity(category=F&B, description=Random Text, amenities=[Cafe, Bar, Rastaurant])])])
But basic-info tag is missed, I could find out why.
Related
The browser sends the following object to the backend:
Now I would like to store the data in my database. So I have an entity that looks like this:
#Entity
public class NewQuote {
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String description;
#ElementCollection(targetClass = Details.class)
private List<Details> details = new ArrayList<>();
public NewQuote(String description, ArrayList<Details> details) {
this.description = description;
this.details = details;
}
#Embeddable
public class Details {
private String description;
private String label;
public Details(String description, String label) {
this.description = description;
this.label = label;
}
public Details() {}
}
Controller
#PostMapping(value = "/save-quote", produces = MediaType.APPLICATION_JSON_VALUE)
public void saveQuote(#RequestBody Map<String, String> data) {
newQuoteService.saveQuote(data);
}
Service
public void saveQuote(Map<String, String> data) {
JSONObject json = new JSONObject(data);
NewQuote newQuote = new NewQuote(
json.getAsString("description"),
json.getAsString("details")
);
newQuoteRepository.save(newQuote);
}
I am getting an error because json.getAsString("details") should not be a string of course. So how can I turn it to ArrayList<Details>?
Add a DTO to manage your json response. You don't need to explicitly use JSONObject because spring already manage the process of mapping under the wood with Jackson.
Also, it is not a good practice to pass your Entities directly into the controller methods.
NewQuoteDto.java
public class NewQuoteDto {
private Long id;
private String description;
private List<Details> details = new ArrayList<>();
public NewQuoteDto() {
}
// getter and setter or you can use lombok
}
DetailDto.java
public class DetailDto {
private String description;
private String label;
public DetailDto() {}
// getter and setter or you can use lombok
}
Controller
#PostMapping(value = "/save-quote", produces = MediaType.APPLICATION_JSON_VALUE)
public void saveQuote(#RequestBody NewQuoteDto dataDto) {
// here you map dataDto to your model before you call saveQuote
NewQuote data = mapper.map(dataDto, NewQuote.class); // if you use ModelMapper library.
newQuoteService.saveQuote(data);
}
For custom mapping take look here.
I have a structure that looks like this for RechargeResponse Model:
public class RechargeResponse {
private String code;
private String status;
private Set<OperatorWiseCircles> payload;
// setter and getters
}
here is the OperatorWiseCircles Model
public class OperatorWiseCircles {
private String operatorName;
private String operatorId;
private List<CircleWisePlan> circleWisePlanLists;
//setter and getters
}
CircleWisePlan Model class
public class CircleWisePlan {
private String circleName;
private String circleId;
}
Below is the sample json which we need to flattern.
{
"code": 200,
"status": "SUCCESS",
"payload": [
{
"operatorName": "VODAFONE",
"operatorId": "VF",
"circleWisePlanLists": [
{
"circleName": "C1",
"circleId": "1"
},
{
"circleName": "C2",
"circleId": "2"
}
]
}
]
}
I am expecting this to be flattern and map it to Entity object, so that I can add all these iteration to Hashset and save them all to DB, I want to do it using java8 stream. I how can I do it efficiently. I didnt get the right example, to parse nested json values and create entities for it using map/ flatmap.
Result should be like
Eg: ["VODAFONE","VF","C1", "1"]--->
ENTRY1
["VODAFONE","VF","C2", "2"] ---> ENTRY2
#Entity
public class RechargePlanEntity extends Audit<String>{
#Id
#Column(name="id")
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
#Column(name="operator_name")
private String operatorName;
#Column(name="operator_id")
private String operatorId;
#Column(name="circle_name")
private String circleName;
#Column(name="circle_id")
private String circleId;
}
Truth is I'm sure is there any easy way to do this, Yet you can follow something like this,
Here in this example I have created utility class to map OperatorWiseCircles class to List<RechargePlanEntity>.
public class Main {
public static void main(String[] args) throws IOException {
String s = "{\"code\":200,\"status\":\"SUCCESS\",\"payload\":[{\"operatorName\":\"VODAFONE\",\"operatorId\":\"VF\",\"circleWisePlanLists\":[{\"circleName\":\"C1\",\"circleId\":\"1\"},{\"circleName\":\"C2\",\"circleId\":\"2\"}]}]}";
ObjectMapper om = new ObjectMapper();
RechargeResponse response = om.readValue(s, RechargeResponse.class);
List<RechargePlanEntity> collection = response.getPayload()
.stream()
.map(MapUtil::toEntity)
.flatMap(Collection::stream)
.collect(Collectors.toList());
System.out.println(collection);
}
}
#Getter
#Setter
#ToString
class RechargePlanEntity {
private Long id;
private String operatorName;
private String operatorId;
private String circleName;
private String circleId;
}
#Getter
#Setter
class RechargeResponse {
private String code;
private String status;
private Set<OperatorWiseCircles> payload;
}
#Getter
#Setter
class OperatorWiseCircles {
private String operatorName;
private String operatorId;
private List<CircleWisePlan> circleWisePlanLists;
}
#Getter
#Setter
class CircleWisePlan {
private String circleName;
private String circleId;
}
final class MapUtil {
public static List<RechargePlanEntity> toEntity(OperatorWiseCircles in) {
return in.getCircleWisePlanLists()
.stream()
.map(MapUtil::map)
.peek(out -> map(in, out))
.collect(Collectors.toList());
}
private static RechargePlanEntity map(CircleWisePlan in) {
RechargePlanEntity out = new RechargePlanEntity();
out.setCircleId(in.getCircleId());
out.setCircleName(in.getCircleName());
return out;
}
private static void map(OperatorWiseCircles in, RechargePlanEntity out) {
out.setOperatorId(in.getOperatorId());
out.setOperatorName(in.getOperatorName());
}
}
The entities without ids may be created from RechargeResponse model providing that the entity has all-args constructor:
RechargeResponse modelFromJson = ... //
List<RechargePlanEntity> entities = modelFromJson.getPayload()
.stream() // Stream<OperatorWiseCircles>
.flatMap(ows -> ows.getCircleWisePlanLists()
.stream() // Stream<CircleWisePlan>
.map(cwp -> new RechargePlanEntity(
null, // instead of id
ows.getOperatorName(),
ows.getOperatorId(),
cwp.getCircleName(),
cwp.getCircleId()
)) // Stream<RechargePlanEntity>
) // Stream<RechargePlanEntity>
.collect(Collectors.toList());
or, if a builder is implemented in the entity class (e.g. using Lombok's #Builder annotation), this conversion may look as follows:
List<RechargePlanEntity> entities = modelFromJson.getPayload()
.stream() // Stream<OperatorWiseCircles>
.flatMap(ows -> ows.getCircleWisePlanLists()
.stream() // Stream<CircleWisePlan>
.map(cwp -> RechargePlanEntity.builder()
.operatorName(ows.getOperatorName())
.operatorId(ows.getOperatorId())
.circleName(cwp.getCircleName())
.circleId(cwp.getCircleId())
.build()
) // Stream<RechargePlanEntity>
) // Stream<RechargePlanEntity>
.collect(Collectors.toList());
I would like to deserialize and map to class following values by name attribute.
This is piece of my XML file.
<custom-attributes>
<custom-attribute name="Name1" dt:dt="string">VALUE</custom-attribute>
<custom-attribute name="Name2" dt:dt="string">
<value>1111</value>
<value>1111</value>
<value>1111</value>
</custom-attribute>
<custom-attribute name="Name3" dt:dt="string">VALUE2</custom-attribute>
<custom-attribute dt:dt="boolean" name="Name3">VALUE3</custom-attribute>
<custom-attribute dt:dt="boolean" name="Name4">VALUE4</custom-attribute>
</custom-attributes>
And This is piece of my pojo class
#JsonIgnoreProperties(ignoreUnknown = true)
public class CustomAttributes {
#JacksonXmlProperty(localName="name3", isAttribute = true)
private String roleID;
public String getRoleID() {
return roleID;
}
public void setRoleID(String roleID) {
this.roleID = roleID;
}
}
Do you know ho to properly read values from those attribues by name ? Currently im receiving null
I am not sure what the result is supposed to look like, but if you want
to parse the complete xml into matching objects they would look like this:
public class CustomAttributeList {
#JacksonXmlProperty(localName = "custom-attributes")
private List<CustomAttributes> list;
...
}
#JacksonXmlRootElement(localName = "custom-attribute")
public class CustomAttributes {
// the name attribute
#JacksonXmlProperty
private String name;
// the datatype from the dt:dt field
#JacksonXmlProperty(namespace = "dt")
private String dt;
// the content between the tags (if available)
#JacksonXmlText
private String content;
// the values in the content (if available)
#JacksonXmlProperty(localName = "value")
#JacksonXmlElementWrapper(useWrapping = false)
private List<String> values;
...
}
Note that the localName="name3" from your question is not referring to a property at all.
Is it possible with Jackson to deserialize json with Builder pattern as well as with default setter and getter approach?
My object is created with Builder that covers only required (final) fields, but I have non-final fields with some values as well that need to be deserialized with setters.
Here is the sample that throws an exception in an attempt to deserialize it with:
new ObjectMapper().readValue(json, Foo.class);
json - json representation serialized with default Jackson serializer, like:
objectMapper.writeValueAsString(foo);
class
#Getter
#Setter
#ToString
#JsonInclude(JsonInclude.Include.NON_NULL)
#JsonDeserialize(builder = Foo.Builder.class)
public class Foo {
private final String key;
private final Long user;
private final String action;
private final String material;
private final String currency;
private Foo(String key, Long user, String action, String material, String currency) {
this.key = key;
this.user = user;
this.action = action;
this.material = material;
this.currency = currency;
}
public static class Builder {
private String key;
private Long user;
private String action;
private String material;
private String currency;
#JsonProperty("key")
public Foo.Builder withKey(String key) {
this.key = key;
return this;
}
#JsonProperty("user")
public Foo.Builder withUser(Long user) {
this.user = user;
return this;
}
#JsonProperty("action")
public Foo.Builder withAction(String action) {
this.action = action;
return this;
}
/// other 'with' setters....
}
#JsonProperty("state")
private int state;
#JsonProperty("stat")
private String stat;
#JsonProperty("step")
private String step;
}
The exception it throws like :
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:
Unrecognized field "state" (class com.Foo$Builder), not marked as
ignorable (5 known properties: "key", "user", "action", "material",
"currency",])
If not possible what workaround is the cheapest?
Two things that are suspicious:
You are willing to use the builder inside the Foo class. In that case you should correct the specification
(SessionData.Builder.class is not correct in that case).
You are indeed trying to use an external builder. In this case you should remove or at least mark as ignorable the inner builder, this seems to be the reason of the excetpion you are getting.
In both cases you should make sure the final method to get the Foo instance is called build() otherwise you should annotate the builder with a #JsonPOJOBuilder(buildMethodName = "nameOfMethod", withPrefix = "set").
I am trying to send a REST request and parse the response, but it returns NullPointerException. I assume the issue is with namespace.
When I have package-info.java file, it returns NullPointerException and when I do not it returns following:
Exception in thread "main"
org.springframework.http.converter.HttpMessageNotReadableException:
Could not unmarshal to [class com.expedia.HotelListResponse]: unexpected
element (uri:"http://v3.hotel.wsapi.ean.com/", local:"HotelListResponse").
Expected elements are <{}ChargeableRateInfo>,<{}HotelList>,
<{}HotelListResponse>,<{}HotelSummary>,<{}NightlyRate>,
<{}NightlyRatesPerRoom>,<{}RateInfo>,<{}RoomRateDetails>,
<{}RoomRateDetailsList>,<{}Surcharge>,<{}Surcharges>,<{}ValueAdd>,
<{}ValueAdds>; nested exception is javax.xml.bind.UnmarshalException:
unexpected element (uri:"http://v3.hotel.wsapi.ean.com/",
local:"HotelListResponse"). Expected elements are <{}ChargeableRateInfo>,
<{}HotelList>,<{}HotelListResponse>,<{}HotelSummary>,<{}NightlyRate>,
<{}NightlyRatesPerRoom>,<{}RateInfo>,<{}RoomRateDetails>,
<{}RoomRateDetailsList>,<{}Surcharge>,<{}Surcharges>,<{}ValueAdd>,
<{}ValueAdds>
If I remove qualified part of package-info the code shows following:
>>>0ABD9-732-A12-124-2BE4FD21A
>>>1
nullPointerException
....
Sample partial response
<ns2:HotelListResponse>
<customerSessionId>0ABD9-732-A12-124-2BE4FD21A
</customerSessionId>
<numberOfRoomsRequested>1</numberOfRoomsRequested>
<moreResultsAvailable>true</moreResultsAvailable>
<cacheKey>168951a:159922be3fd:-11ce</cacheKey>
<cacheLocation>19.11.13.19:7300</cacheLocation>
<HotelList size="20" activePropertyCount="1231">
<HotelSummary order="0">
<hotelId>335698</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>1511947</amenityMask>
<locationDescription>Near London Aquarium</locationDescription>
<shortDescription><p><b>Property Location</b>
<br />A stay at Park Plaza Westminster Bridge London places
you in the heart of London, steps from London Aquarium and London
Dungeon. This 4-star hotel is close to</shortDescription>
<highRate>314.27</highRate>
<lowRate>254.35</lowRate>
<rateCurrencyCode>USD</rateCurrencyCode>
<latitude>51.50111</latitude>
<longitude>-0.11733</longitude>
<proximityDistance>0.7890795</proximityDistance>
<proximityUnit>MI</proximityUnit>
<hotelInDestination>true</hotelInDestination>
<thumbNailUrl>/hotels/4000000/3120000/3113100/3113039/3113039_31_t.jpg
</thumbNailUrl>
<deepLink>http://www.travelnow.com/templates/441384/hotels/335698/overview?lang=en¤cy=USD&standardCheckin=11/23/2016&standardCheckout=11/25/2016&roomsCount=1&rooms[0].adultsCount=1
</deepLink>
<RoomRateDetailsList>
<RoomRateDetails>
<roomTypeCode>200750627</roomTypeCode>
<rateCode>203729567</rateCode>
<maxRoomOccupancy>2</maxRoomOccupancy>
<quotedRoomOccupancy>1</quotedRoomOccupancy>
<minGuestAge>0</minGuestAge>
<roomDescription>Superior Twin Room, 2 Single Beds
</roomDescription>
<promoId>209171300</promoId>
<promoDescription>Save 15%</promoDescription>
<currentAllotment>9</currentAllotment>
<propertyAvailable>true</propertyAvailable>
<propertyRestricted>false</propertyRestricted>
<expediaPropertyId>3113039</expediaPropertyId>
<rateKey>81300a5d-b697-457e-a059-2c22f6ce389b</rateKey>
<RateInfo priceBreakdown="true" promo="true" rateChange="true">
<ChargeableRateInfo averageBaseRate="263.30"
averageRate="217.28" commissionableUsdTotal="521.48"
currencyCode="USD" maxNightlyRate="222.61" nightlyRateTotal="434.57"
surchargeTotal="86.91" total="521.48">
<NightlyRatesPerRoom size="2">
<NightlyRate baseRate="269.75" rate="222.61" promo="true" />
<NightlyRate baseRate="256.84" rate="211.96" promo="true" />
</NightlyRatesPerRoom>
<Surcharges size="1">
<Surcharge type="TaxAndServiceFee" amount="86.91" />
</Surcharges>
</ChargeableRateInfo>
</RateInfo>
<ValueAdds size="1">
<ValueAdd id="2048">
<description>Free Wireless Internet</description>
</ValueAdd>
</ValueAdds>
</RoomRateDetails>
</RoomRateDetailsList>
</HotelSummary>
<HotelSummary order="1">
Request
final String URL = "http://api.ean.com/ean-services/rs/hotel/v3/list?cid="+CID+ "&apikey=" +API_KEY +"&sig= " + sig + "&apiExperience=PARTNER_WEBSITE&arrivalDate=12/11/2016&departureDate=12/18/2016&room1=1&city=London&stateProvinceCode=LN&countryCode=UK";
System.err.println("URL:" + URL);
RestTemplate restTemplate = new RestTemplate();
HotelListResponse hotelResponse = restTemplate.getForObject(URL,HotelListResponse.class);
System.err.println(">>>" + hotelResponse.getCacheKey());
System.err.println(">>>" + hotelResponse.getNumberOfRoomsRequested());
System.err.println(" Hotel Summaries Size>>>" + hotelResponse.getHotelList().getHotelSummaries().size());
package-info.java
#XmlSchema(
namespace = "http://v3.hotel.wsapi.ean.com/",
elementFormDefault = XmlNsForm.QUALIFIED)
package com.expedia;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
HotelListResponse
#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;
getters and setters go here
HotelList
#XmlRootElement(name="HotelList")
#XmlAccessorType(XmlAccessType.FIELD)
public class HotelList {
#XmlAttribute(name="size")
private int size;
#XmlAttribute(name="activePropertyCount")
private int activePropertyCount;
private List<HotelSummary> hotelSummaries;
getters and setters go here
HotelSummary
#XmlRootElement(name="HotelSummary")
#XmlAccessorType(XmlAccessType.FIELD)
public class HotelSummary {
#XmlAttribute(name="order")
private int order;
#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="stateProvinceCode")
private String stateProvinceCode;
#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="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;
getters and setters go here
RoomRateDetailsList
#XmlRootElement(name = "RoomRateDetailsList")
#XmlAccessorType(XmlAccessType.FIELD)
public class RoomRateDetailsList {
#XmlElement(name = "RoomRateDetails")
private RoomRateDetails roomRateDetails;
getters and setters go here
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 = "currentAllotment")
private int currentAllotment;
#XmlElement(name = "propertyAvailable")
private boolean propertyAvailable;
#XmlElement(name = "propertyRestricted")
private boolean propertyRestricted;
#XmlElement(name = "expediaPropertyId")
private int expediaPropertyId;
#XmlElement(name = "rateKey")
private String rateKey;
#XmlElement(name="RateInfo")
private RateInfo rateInfo;
#XmlElement(name="ValueAdds")
private ValueAdds valueAdds;
getters and setters go here
RateInfo
#XmlRootElement(name="RateInfo")
#XmlAccessorType(XmlAccessType.FIELD)
public class RateInfo {
#XmlAttribute(name="priceBreakdown")
private boolean priceBreakdown;
#XmlAttribute(name="promo")
private boolean promo;
#XmlAttribute(name="rateChange")
private boolean rateChange;
#XmlElement(name="ChargeableRateInfo")
private ChargeableRateInfo chargeableRateInfo;
getters and setters go here
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;
getters and setters go here
NightlyRatesPerRoom
#XmlRootElement(name = "NightlyRatesPerRoom")
#XmlAccessorType(XmlAccessType.FIELD)
public class NightlyRatesPerRoom {
#XmlAttribute(name = "size")
private int size;
#XmlElement(name = "NightlyRate")
private List<NightlyRate> nightlyRates;
getters and setters go here
NightlyRate
#XmlRootElement(name = "NightlyRate")
#XmlAccessorType(XmlAccessType.FIELD)
public class NightlyRate {
#XmlAttribute(name = "baseRate")
private double baseRate;
#XmlAttribute(name = "rate")
private double rate;
#XmlAttribute(name = "promo")
private boolean promo;
getters and setters go here
Surcharges
#XmlRootElement(name = "Surcharges")
#XmlAccessorType(XmlAccessType.FIELD)
public class Surcharges {
#XmlAttribute(name = "size")
private int size;
#XmlElement(name = "Surcharge")
private List<Surcharge> surcharges;
getters and setters go here
Surcharge
#XmlRootElement(name = "Surcharge")
#XmlAccessorType(XmlAccessType.FIELD)
public class Surcharge {
#XmlAttribute(name = "type")
private String type;
#XmlAttribute(name = "amount")
private double amount;
getters and setters go here
ValueAdds
#XmlRootElement(name = "ValueAdds")
#XmlAccessorType(XmlAccessType.FIELD)
public class ValueAdds {
#XmlElement(name = "size")
private int size;
#XmlElement(name = "ValueAdd")
private ValueAdd valueAdd;
getters and setters go here
ValueAdd
#XmlRootElement(name = "ValueAdd")
#XmlAccessorType(XmlAccessType.FIELD)
public class ValueAdd {
#XmlElement(name = "description")
private String description;
getters and setters go here
Your schema declares elementFormDefault="qualified" as we can see in your package-info, so you must remove the ns2 prefix at the root Element, or you must add it to all the inner elements, depends on the xmlns declaration (that we can't see).