Question

Exists a way by using JAXB v.2.2.7 to specify the marshalling/unmarshalling of an object from a class defined in an external library? I guess that an adapted must be defined, but how can I notice that the object has my adapter?

E.g. I use Interval, from Joda library. If I annotate the Interval object into a class as follows:

@XmlElement(name="DateInterval")
protected Interval dateInterval;

Then an empty tag is created during the marshalling process, i.e.

<DateInterval/>

Of course, this was expected since none annotated the Interval class.

Was it helpful?

Solution

For the Joda library you can create a JAXB XmlAdapter for types such as Interval. Then you can reference that XmlAdapter at the package level so that all mapped fields/properties of that type that belong to classes in that package will apply the XmlAdapter.

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdapter(type=DateTime.class,
        value=DateTimeAdapter.class),
    @XmlJavaTypeAdapter(type=DateMidnight.class,
        value=DateMidnightAdapter.class),
    @XmlJavaTypeAdapter(type=LocalDate.class,
        value=LocalDateAdapter.class),
    @XmlJavaTypeAdapter(type=LocalTime.class,
        value=LocalTimeAdapter.class),
    @XmlJavaTypeAdapter(type=LocalDateTime.class,
        value=LocalDateTimeAdapter.class)
})
package blog.jodatime;

import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapters;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.LocalTime;

For More Information

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top