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).
Solution for my IDE
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>
Solution for the JAR
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.
Resources Directory
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.
POM Dependencies
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>
UPDATES for MOXy version 3
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:
- Change the
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.
- Change all of your jaxb imports to refer to
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
- Use the following two dependencies in your
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.