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.
-
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 theCustomer.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>
- Make sure all your classes (in this case
Customer.class
andDemo.ckass
) are using the imports fromimport 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
.
- Add the following
<build>
to yourpom.xml
:
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
</build>
- 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:
-
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 instep-2
would be sufficient. -
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 afterJava 11
better use the latest version and the steps mentioned in this answer or the other answer provided with the link above. -
Make sure you clean and rebuild the maven project because sometimes the
dependencies
are not directly added when you save thepom.xml
file. At least in my case when I was using theIntellij IDE
. I was doing clean and build every time I added the dependency so that I am making sure everything is fine fromMaven
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.