Hi All,
I am having an issue where JAXB is not marshalling the derived object
attributes as i want it to. Let me explain with an example.
I have a wrapper object as follows which i am returning as part of a jersey
call.
@XmlRootElement(name = "effectiveperiods")
public class EffectivePeriodsWrapper {
private List<EffectivePeriod> effectivePeriods;
public EffectivePeriodsWrapper(List<EffectivePeriod> result) {
effectivePeriods = result;
}
public EffectivePeriodsWrapper() {
}
/*
* The Wrapper and its element.
*/
@XmlElement(name = "effectivePeriod")
@XmlElementWrapper(name = "effectivePeriods")
public List<EffectivePeriod> getEffectivePeriods() {
return effectivePeriods;
}
public void setEffectivePeriods(List<EffectivePeriod> effectivePeriods)
{
this.effectivePeriods = effectivePeriods;
}
}
This deals with EffectivePeriod which is as follows
@XmlRootElement(name="effectivePeriod")
public class EffectivePeriod // this is a jersey resource
{
private Date startDate;
private Date endDate;
public EffectivePeriod(EffectivePeriod result) {
this.startDate=result.getStartDate();
this.endDate=result.getEndDate();
}
public EffectivePeriod(Date theStartDate,Date theEndDate) {
this.startDate = theStartDate;
this.endDate = theEndDate;
}
public EffectivePeriod(){
}
@XmlJavaTypeAdapter(DateAdapter.class)
public Date getStartDate() {
return startDate;
}
@XmlJavaTypeAdapter(DateAdapter.class)
public Date getEndDate() {
return endDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
}
I have a PersonPeriod which extends this as follows
@XmlRootElement(name="effectivePeriod")
@XmlAccessorType(XmlAccessType.FIELD)
public class PersonPeriod extends EffectivePeriod {
@XmlElement(required = true)
private String reason;
public String getReason() {
return reason;
}
public PersonPeriod(){}
public void setReason(String reason) {
this.reason = reason;
}
public PersonPeriod(EffectivePeriod result) {
super(result);
}
}
This just adds reason attribute to the base EffectivePeriod .
I have one more resource which returns a list of EffectivePeriods resources
in its Get(kind of returning periods) wrapped in a EffectivePeriodWrapper.
Since PersonPeriod is a EffectivePeriod(since it derives), i create a list
of these to be returned in the Get.
In a get of some resource i do the following
@GET
@Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("periods")
public EffectivePeriodsWrapper getXml() {
for all periods{
EffectivePeriod period = createEffectivePeriod(e);// This goes
down to the derived class which creates PersonPeriod
result.add(period);
}
return new EffectivePeriodsWrapper(result);// The result is list of
EffectivePeriods which has PersonPeriod elements in it
}
When i get the json or xml on the other end I dont see the "reason"
attribute which is in the PersonPeriod even though at the point of the
return i see that in debugger. What could be the reason? Why is JAXB not
going down the EffectivePeriod hierarchy and marshalls it based on the
object it contains rather than the base object?
Thx
Vaidya