开发者

Serialization of JodaTime Period with Jaxb (BasePeriod does not have a default constructor)

开发者 https://www.devze.com 2023-04-11 18:40 出处:网络
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.c

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号