今天做Webservice项目时,传递datatime类型的数据时,提示java.sql.timestamp does not have a no-arg default constructor~~~~
百度一下,好像是java.sql.Timestamp 无法和xml绑定。这个时候我们要写一个继承自XmlAdapter的adapter类,如下:
import java.sql.Timestamp;
import java.util.Date;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class TimestampAdapter extends XmlAdapter<Date, Timestamp> {
public Date marshal(Timestamp t) {
return new Date(t.getTime());
}
public Timestamp unmarshal(Date d) {
return new Timestamp (d.getTime());
}
}
然后使用@XmlJavaTypeAdapter标记到使用java.sql.Timestamp类的地方,例如:
@XmlRootElement
public class InfoDTO {
private Timestamp createTime;
...
@XmlJavaTypeAdapter(TimestampAdapter.class)
public Timestamp getCreateTime() {
return this.createTime;
}
}
引用:http://alimama.iteye.com/blog/848895
本文介绍了一种解决Java中Timestamp类型在XML绑定时出现的问题的方法。通过创建一个自定义的适配器类TimestampAdapter,继承自XmlAdapter并实现对Timestamp类型的转换,最终成功将Timestamp类型与XML进行绑定。
2442

被折叠的 条评论
为什么被折叠?



