javamoxy

How to use instantiated XmlAdapter in MOXy?


When using JAXB without MOXY I did Unmarshal like below.

          Unmarshaller u = this.jaxbContext.createUnmarshaller();
          u.setAdapter(new Something.JaxbAdapter(this, ...));

However, when using MOXy's JAXBContextFactory, an error occurs at the time of createContext.

Because of @XmlJavaTypeAdapter(Something.JaxbAdapter.class).

this.jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(classes.toArray(new Class[0]), Collections.emptyMap());

Error:

Exception [EclipseLink-50063] (Eclipse Persistence Services - 3.0.2.v202107160933): org.eclipse.persistence.exceptions.JAXBException
Exception Description: An exception occurred while attempting to instantiate XmlAdapterClass [....Somegh$JaxbAdapter]. A possible cause is that the adapter class has no zero argument constructor.
Internal Exception: java.lang.InstantiationException: ....Somegh$JaxbAdapter

How to use instantiated XmlAdapter in MOXy?


Solution

  • This was not possible with MOXY.

    I used spring-oxm's Jaxb2Marshaller instead of MOXy to get it working.