I am using Moxy Implementation of JAXB in my codeset and trying to create paths using @XMLPath but it seems to be not working. I have a Spring bassed Project and I have created jaxb.properties under /project/WEB/src/main/resources having content :
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
My classes are placed under /project/WEB/src/main/java I have configured my pom.xml to download the dependancy. persistence.moxy -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.4.0</version>
</dependency>
When I run the code on WAS server, Moxy is not recongnized and the path is not created. Not sure what am I doing wrong.
I even tried testing my JAXBContext class but what I get on the console is :
JAXBContext jaxbContext = JAXBContext.newInstance(DocGenerator.class);
System.out.println(jaxbContext.getClass());
class com.sun.xml.bind.v2.runtime.JAXBContextImpl
Can someone please help ?
In case this helps anyone else, the following fixed my recent "MOXy is configured but is being ignored" problem.
I tested it on Java 11 (Open JDK) by implementing the first example shown in the @XmlPath Javadoc.
In line with the instructions here, I added the required properties file to the package containing my domain classes (the annotated beans which I wanted to serialize).
The step I initially forgot to do was this:
My IDE only copies compiled .class
files to the target directory - it does not copy anything else (such as my properties file).
Duh.
How you fix that depends on your IDE. For me, it was a simple change to the ant build configuration for my project.
If you are using Maven, then the build step in the pom.xml
would be as follows:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>
Similarly, Maven needs to be instructed to copy the file to that folder in your JAR. There are various ways to do this. See this question for some answers.
Placing this specific config file into a resources
directory does not work (at least, it did not work for me). The MOXy instructions are quite specific - and need to be followed exactly.
In Java 11, the module java.se.ee has been removed. See JEP-320. This module includes JAXB (and JAX-WS). To use JAXB in Java 11 and newer, you need to add it to your project as a separate library.
Given that, here are my POM dependencies, for the record:
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
<!--
Use 2.3.1 below to prevent "illegal
reflective access operation" warnings.
-->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.7.6</version>
</dependency>
The above notes work for MOXy version 2.7.6.
To work with version 3+, you need to take into account the recent transfer of many javax
packages to Jakarta. You can read the background to this move here.
Steps to upgrade your project:
jaxb.properties
file to the following:jakarta.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Note the changed reference to jakarta
here, for the bind context.
jakarta
instead of javax
. So, for example, change this:import javax.xml.bind.JAXBContext; // old import
to this:
import jakarta.xml.bind.JAXBContext; // new import
pom.xml
:<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>3.0.0</version>
</dependency>
These two dependencies are sufficient to support an updated version of the code example presented here: XPath Based Mapping using MOXy.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With