使用 Jaxb 序列化 JodaTime period(BasePeriod 没有默认构造函数)

发布于 2024-12-09 07:22:33 字数 1347 浏览 1 评论 0原文

我编写了一个适配器来处理周期的序列化 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕嗳→ 2024-12-16 07:22:33

问题已解决。我在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文