Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not able to configure Moxy using JAXB

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 ?

like image 961
shivam-shekhar Avatar asked Oct 30 '22 20:10

shivam-shekhar


1 Answers

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:

  1. 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.

  1. 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
  1. 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.

like image 121
andrewJames Avatar answered Nov 17 '22 22:11

andrewJames