当Jaxb中存在Timestamp类型(任何Bean中包含Timestamp)的话,容器启动时会出现一个运行时错误,说Timestamp没有一个默认的构造器。解决这个问题的办法如下:
(1)写一个Timestamp Adapter:
(2)在Bean的get方法中加入:
(1)写一个Timestamp Adapter:
import java.util.Date;
import java.sql.Timestamp;
import javax.xml.bind.annotation.adapters.XmlAdapter;
public class TimestampAdapter extends XmlAdapter<Date, Timestamp> {
public Timestamp unmarshal(Date val) throws Exception {
return new Timestamp(val.getTime());
}
public Date marshal(Timestamp val) throws Exception {
return new Date(val.getTime());
}
}
(2)在Bean的get方法中加入:
@Column(name = "TIME_UPDATED", length = 19)
@XmlJavaTypeAdapter(TimestampAdapter.class) //加入这一行
public Timestamp getTimeUpdated() {
return this.timeUpdated;
}
本文介绍了一种解决JAXB在处理Timestamp类型时出现运行时错误的方法。通过创建一个TimestampAdapter适配器类,并在Bean的get方法中标注@XmlJavaTypeAdapter,可以避免因Timestamp缺乏默认构造器而导致的问题。
33

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



