使用 Jaxb 序列化 JodaTime period(BasePeriod 没有默认构造函数)
我编写了一个适配器来处理周期的序列化 https ://www.joda.org/joda-time/apidocs/org/joda/time/Period.html in JodaTime with JAXB as it said in http://blog.bdoughan.com/ 2011/05/jaxb-and-joda-time-dates-and-times.html 但它不起作用。
public class PeriodAdapter extends XmlAdapter<String, Period>{
@Override
public Period unmarshal(String p) throws Exception {
return new Period(p);
}
@Override
public String marshal(Period p) throws Exception {
return p.toString();
}
然后
在我需要使用适配器的类中,我使用注释
public class ActiveHistorySettings {
private Period maximumPeriod;
@Min(0)
private int maximumAccesses;
@XmlJavaTypeAdapter(PeriodAdapter.class)
public Period getMaximumPeriod() {
return this.maximumPeriod;
}
如果我调试应用程序,则在尝试解组我的 xml 之前未使用适配器...
这是堆栈跟踪
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of
IllegalAnnotationExceptions
org.joda.time.base.BasePeriod does not have a no-arg default constructor.
this problem is related to the following location:
at org.joda.time.base.BasePeriod
at org.joda.time.Period
I have written an adapter to handle serialization of a Period https://www.joda.org/joda-time/apidocs/org/joda/time/Period.html in JodaTime with JAXB as it says in http://blog.bdoughan.com/2011/05/jaxb-and-joda-time-dates-and-times.html but it doesn't work.
public class PeriodAdapter extends XmlAdapter<String, Period>{
@Override
public Period unmarshal(String p) throws Exception {
return new Period(p);
}
@Override
public String marshal(Period p) throws Exception {
return p.toString();
}
}
and then in my class where I need to use the adapter I use the annotation
public class ActiveHistorySettings {
private Period maximumPeriod;
@Min(0)
private int maximumAccesses;
@XmlJavaTypeAdapter(PeriodAdapter.class)
public Period getMaximumPeriod() {
return this.maximumPeriod;
}
If I debug the application the adapter is not been used before trying to unmarshall my xml ...
this is the stacktrace
com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of
IllegalAnnotationExceptions
org.joda.time.base.BasePeriod does not have a no-arg default constructor.
this problem is related to the following location:
at org.joda.time.base.BasePeriod
at org.joda.time.Period
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题已解决。我在 Eclipse 中的构建路径有问题。事实上,我没有使用 JAXB 库。现在我正在使用 jaxb 2.2.4 http://jaxb.java.net/ ,当我调试应用程序我的适配器已被使用:D
The problem is fixed. I had a problem with my build path in eclipse. In fact, I was not using JAXB library. Now I'm using jaxb 2.2.4 http://jaxb.java.net/ and when I debug the application my adapter it's been used :D