I am writing a JAX-RS (Jersey) webservice that is an interface to JPA (EclipseLink) entities for CRUD operations. I am using JAXB (EclipseLink) to convert the entities to/from XML in the requests/responses. I created this class to marshall/unmarshall the Timestamp fields in my entities:
public class TimestampXmlAdapter extends XmlAdapter<String, Timestamp>
{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
public static String format(Timestamp timestamp)
{
String formatted = "";
if (timestamp != null)
{
formatted = dateFormat.format(timestamp);
}
return formatted;
}
@Override
public String marshal(Timestamp timestamp) throws Exception
{
return TimestampXmlAdapter.format(timestamp);
}
@Override
public Timestamp unmarshal(String timestamp) throws Exception
{
return new Timestamp(dateFormat.parse(timestamp).getTime());
}
}
This works fine for the marshalling of the entities, but I am getting errors on the unmarshallig if the Timestamp element is empty. For example:
<MyObject>
<id>111-222</id>
<activeDate></activeDate>
</MyObject>
generates this error:
Local Exception Stack:
Exception [EclipseLink-3001] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [], of class [class java.lang.String], could not be converted to [class java.sql.Timestamp].
at org.eclipse.persistence.exceptions.ConversionException.couldNotBeConverted(ConversionException.java:71)
So, I updated the unmarshall method to:
public Timestamp unmarshal(String timestamp) throws Exception
{
if (timestamp == null || timestamp.equals(""))
{
return null;
}
return new Timestamp(dateFormat.parse(timestamp).getTime());
}
and that generates this error:
Local Exception Stack:
Exception [EclipseLink-33] (Eclipse Persistence Services - 2.0.1.v20100213-r6600): org.eclipse.persistence.exceptions.DescriptorException
Exception Description: Trying to invoke [setActiveDate] on the object with the value []. The number of actual and formal parameters differs, or an unwrapping conversion has failed.
Internal Exception: java.lang.IllegalArgumentException: argument type mismatch
Mapping: org.eclipse.persistence.oxm.mappings.XMLDirectMapping[activeDate-->activeDate/text()]
Descriptor: XMLDescriptor(my.package.model.MyObject --> [DatabaseTable(MyObject)])
at org.eclipse.persistence.exceptions.DescriptorException.illegalArgumentWhileSettingValueThruMethodAccessor(DescriptorException.java:691)
Can anyone help me fix this?