JAXB throws error-arg-constructor for @XmlTransient field
I have a class that declares a Joda Time field DateTime
.
However, the value for this parameter is not set by the unmarhsalling process; instead, it is set later in the method afterUnmarhsal
.
Therefore the box is marked XmlTransient
:
@XmlTransient
private DateTime startTime;
However, when trying to run, I ran into this error:
javax.xml.bind.JAXBException: Exception Description: Class org.joda.time.DateTime $ Property requires a null-argument constructor or specified factory method. Note that non-static inner classes do not have null-argument constructors and are not supported. - with an associated exception: [Exception [EclipseLink-50001] (Eclipse Persistence Services - 2.2.0.v20110202-r8913) ...
Why should JAXB care about this class, given that it is clearly transitory for marching and non-marshalling purposes?
How can I tell JAXB to ignore this field? I know I could use a factory method there, but it seems pointless given that the factory won't be able to instantiate the value (so it did it in afterUnmarshal
)
source to share
A bug has been found in EclipseLink 2.2.0 that has been fixed in subsequent releases of EclipseLink. You will still see this exception in the latest release of EclipseLink if you use default access ( XmlAccessType.PUBLIC
), but annotate this field:
package forum9610190;
import javax.xml.bind.annotation.XmlTransient;
import org.joda.time.DateTime;
public class Root {
@XmlTransient
private DateTime startTime;
public DateTime getStartTime() {
return startTime;
}
public void setStartTime(DateTime startTime) {
this.startTime = startTime;
}
}
Option # 1 - annotate the property
Instead of an annotation field, you can annotate a property (get method) with @XmlTransient
:
package forum9610190;
import javax.xml.bind.annotation.XmlTransient;
import org.joda.time.DateTime;
public class Root {
private DateTime startTime;
@XmlTransient
public DateTime getStartTime() {
return startTime;
}
public void setStartTime(DateTime startTime) {
this.startTime = startTime;
}
}
Option # 2 - annotate the field and use @XmlAccessorType (XmlAccessType.FIELD)
If you want to annotate this field, you need to specify @XmlAccessorType(XmlAccessType.FIELD)
in your class:
package forum9610190;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlTransient;
import org.joda.time.DateTime;
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlTransient
private DateTime startTime;
public DateTime getStartTime() {
return startTime;
}
public void setStartTime(DateTime startTime) {
this.startTime = startTime;
}
}
Additional Information
source to share