Thursday, March 31, 2011

To supress the class attribute 'sql-timestamp' in xstream

I declared createdate as java.util.date in DTO and in the hibernate mapping its been declared as timestamp. So it is trying to marshal a timestamp while datamember is date.
<createDate class="sql-timestamp"> .... </createDate>

To suppress this, either change the date to timestamp in your dto class or
you may set a java.sql.Timestamp as (another) default implementation for a java.util.Date or
you may remove the class attribute completely, something like

StringWriter writer = new StringWriter();
xstream.marshal(this, new PrettyPrintWriter(writer) {
@Override
public void addAttribute(final String key, final String value) {
if (!key.equals("class")) {
super.addAttribute(key, value);
}
}
});

1 comment:

  1. The follwing should fix the issue

    xstream.addDefaultImplementation(java.sql.Date.class,java.util.Date.class); xstream.addDefaultImplementation(java.sql.Timestamp.class,java.util.Date.class); xstream.addDefaultImplementation(java.sql.Time.class,java.util.Date.class);

    ReplyDelete