Not able to configure Moxy using JAXB

六眼飞鱼酱① 提交于 2020-02-21 13:39:45

问题


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 ?


回答1:


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.

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>


来源:https://stackoverflow.com/questions/39388961/not-able-to-configure-moxy-using-jaxb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!