@XmlPath has no impact during the JAXB Marshalling

Finally was able to find the solution with a lot of assist from @andrewjames. Posting the solution for the same so somebody can find the solution quickly and do not end up spending whole day like me 🙂

You can also follow the steps mentioned by @andrewjames in his answer.

  1. Make sure you have only ONE copy of jaxb.properties file within the same package as of the domain class (the one which will be used during the marshalling in this case the Customer.class). The content of the file should be:

    jakarta.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

2.Remove all the dependency from your pom.xml file and just add the following 2 dependencies:

<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>
  1. Make sure all your classes (in this case Customer.class and Demo.ckass) are using the imports from import jakarta.xml.bind.JAXBContext;.

Previously it was taking from import javax.xml.bind.JAXBContext; should be replaced from import jakarta.xml.bind.JAXBContext;.

I was making changes to Demo.class and forgot to change in Customer.class so was seeing some defclass issue. So make sure all your class uses the imports from Jakarta and not from Javax.

  1. Add the following <build> to your pom.xml:
      <build>
        <resources>
          <resource>
            <directory>src/main/java</directory>
            <excludes>
              <exclude>**/*.java</exclude>
            </excludes>
          </resource>
        </resources>
      </build>
  1. After making these changes run the Demo.class this should give you the expected XML. Also @XmlPath should work as expected.

Following are some of the mistakes I was making. You can check if you are not making them:

  1. I was having too many dependencies and some were duplicated also. Such as dependencies from com.sun.activation, javax.activation, javax.validation, org.glassfish.jaxb, etc. These were mentioned in other answers so I was trying everything. Better remove all and make a fresh start and add only the once needed. For this case, only 2 mentioned in step-2 would be sufficient.

  2. The answers mentioned in most of the post on Stackoverflow is for the old version. Including the articles and blogs on various website. As there has been some changes after Java 11 better use the latest version and the steps mentioned in this answer or the other answer provided with the link above.

  3. Make sure you clean and rebuild the maven project because sometimes the dependencies are not directly added when you save the pom.xml file. At least in my case when I was using the Intellij IDE. I was doing clean and build every time I added the dependency so that I am making sure everything is fine from Maven side.

That’s all the points I feel sufficient for making this work. If you are still facing some issue then leave a comment on the other post or here. I will try to answer if I can. Have a nice day.

Leave a Comment