Cheetah
Cheetah

Reputation: 14379

XmlJavaTypeAdapter at package level not working

I have the following in my package-info.java:

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdapter(type=OffsetDateTime.class, value=OffsetDateTimeAdapter.class)
})
package java.time;

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

and the following as an adapter:

package java.time;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class OffsetDateTimeAdapter extends XmlAdapter<String, OffsetDateTime> {

    @Override
    public OffsetDateTime unmarshal(String v) throws Exception {
        return OffsetDateTime.parse(v);
    }

    @Override
    public String marshal(OffsetDateTime v) throws Exception {
        return v.toString();
    }
}

But when I do the following:

    JAXBContext jc = JAXBContext.newInstance(Root.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    JAXBElement<Root> rootElement = unmarshaller.unmarshal(node, Root.class);

I get the following:

com.sun.xml.internal.bind.v2.ClassFactory create0 INFO: No default constructor found on class java.time.OffsetDateTime

Do I have something missing from my setup - its not calling my adapter?

Upvotes: 1

Views: 1072

Answers (1)

laune
laune

Reputation: 31290

java.time is not the package where this should be. Annotations must refer to packages and classes in user space.

Place the package-info.java and OffsetDateTimeAdapter into the package where Root and its children are located.

Upvotes: 1

Related Questions