我正在关注与JAXB编组/解组相关的线程一段时间了。
当我试图将Java对象转换为XML时,我遇到了问题,而XML中有一些接口。后来我发现这可以使用EclipseLink的MOxy来解决。 @ Bdoughan的文章http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html,答案非常有帮助。但是现在,当我试图将生成的相同XML文件解组回Java对象时,我遇到了问题。
这是界面。
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);
}
其中一项实施
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>
因此,当我尝试Unmarshall时,我收到此错误堆栈跟踪..
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 --> [])
它没有默认构造函数,但是在接口中?我真的很欣赏我做错了什么吗?
我是否错过了注释班级中的任何元素?
我的解组码。!
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);
}