I am trying to use JAXB with fields like LocalDateTime . I wrote an adapter to handle the conversion:
public class LocalDateTimeXmlAdapter extends XmlAdapter<String, LocalDateTime> { @Override public String marshal(LocalDateTime arg0) throws Exception { return arg0.toString(); } @Override public LocalDateTime unmarshal(String arg) throws Exception { return LocalDateTime.parse(arg); } }
I registered the adapter in package-info.java as follows:
@XmlJavaTypeAdapters({ @XmlJavaTypeAdapter(type=LocalDateTime.class, value=LocalDateTimeXmlAdapter.class) }) package xml; import java.time.LocalDateTime; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;
This seems sufficient according to this page . However, I keep getting the following error:
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions java.time.LocalDateTime does not have a no-arg default constructor.
I understand the reason for the exception, but I can hardly add a default constructor to java.time.LocalDateTime . This seems to be a disadvantage of the class / weird design solution. Are there any workarounds?
hfhc2 source share