我创建了以下Garde
类,其beginDate
类型endDate
和java.time.LocalDateTime
为字段
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "garde")
public class Garde {
private LocalDateTime beginDate, endDate;
//constructors and methods
}
当我尝试编组Garde
列表时,我得到了这个例外:
No default constructor found on class java.time.LocalDateTime
java.lang.NoSuchMethodException: java.time.LocalDateTime.<init>()
....
我尝试使用no-args构造函数创建另一个扩展java.time.LocalDateTime
的类,但它不起作用,因为LocalDateTime
类是final,那么如何处理呢?
答案 0 :(得分:1)
您可以实施XmlAdapter
并使用@XmlJavaTypeAdapter
配置它。
有些事情:
public class LocalDateTimeAdapter extends
XmlAdapter<String, LocalDateTime> {
public LocalDateTime unmarshal(String value) {
return value == null ? null : LocalDatTime.parse(value);
}
// ...
}
在你的班上:
@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)
private LocaDateTime beginDate;
您还可以将@XmlJavaTypeAdapters({@XmlJavaTypeAdapter(LocalDateTimeAdapter.class)})
注释应用于package-info.java
。