I am following the threads related to JAXB Marshalling / Unmarshalling for a while now.
I had run into problems while i was trying to convert a Java Object into XML, which had some interfaces in it. Later i found out this can be solved using EclipseLink's MOxy. @Bdoughan's articles here http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html and answers were very helpful. But now, i have problems when i am trying to unmarshal the same XML file generated back into Java Objects.
This is the interface.
public interface TimePeriod extends Serializable{
/**
* Return the end Time of the Time Period
*/
public Date getEndDate();
/**
* Return the startTime of the Time Period
*/
public Date getStartDate();
/**
* Sets the startTime of the Time period
*/
public void setStartDate(Date newStartDate);
/**
* Sets the endTime of the Time period
*/
public void setEndDate(Date newEndDate);
}
One of the Implementations
TaskStepTimePeriod
public class TaskStepTimePeriod implements TimePeriod {
private Date taskStartDate;
private Date taskEndDate;
@Override
public Date getEndDate() {
return taskEndDate;
}
@Override
public Date getStartDate() {
return taskStartDate;
}
public void setTaskStartDate(Date newVal) {
this.taskStartDate = newVal;
}
public void setTaskEndDate(Date newVal) {
this.taskEndDate = newVal;
}
@Override
public void setStartDate(Date newStartDate) {
this.taskStartDate = newStartDate;
}
@Override
public void setEndDate(Date newEndDate) {
this.taskEndDate = newEndDate;
}
}
Marshalled XML
<?xml version="1.0" encoding="UTF-8"?>
<plannedProgram>
<taskStep>
<endDate>2014-07-31T12:00:00.262+05:30</endDate>
<startDate>2014-06-01T12:00:00.262+05:30</startDate>
</taskStep>
</plannedProgram>
So when i try to Unmarshall, i get this error stacktrace..
Exception in thread "main" Local Exception Stack:
Exception [EclipseLink-63] (Eclipse Persistence Services - 2.6.1.v20150916-55dc7c3): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: The instance creation method [com.cts.axb.model.TimePeriod.<Default Constructor>], with no parameters, does not exist, or is not accessible.
Internal Exception: java.lang.NoSuchMethodException: com.cts.axb.model.TimePeriod.<init>()
Descriptor: XMLDescriptor(com.cts.axb.model.TimePeriod --> [])
It says no default constructor, but in an interface? Would really appreciate some pointers on what have i done wrong?
Have i missed annotating any elements in the classes?
My Unmarshalling code.!
try {
context = JAXBContext.newInstance(PlannedProgram.class);
Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();
newMaintProg = (PlannedProgram) jaxbUnmarshaller.unmarshal(file);
System.err.println("XML file succesffuly read");
} catch (JAXBException e) {
System.err.println("Error Reading / Unmarshalling the XML file "+ e);
}
Please see https://jaxb.java.net/guide/Mapping_interfaces.html . Sections 3.2.2 and 3.2.3 are the most relevant.
Though, may be you just could specify concrete type in PlannedProgram
, eg
private TaskStepTimePeriod taskStep;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.