How to get maven to use a different JAXB library t

2020-07-14 06:52发布

问题:

I'm using java 1.6.0_14 which includes an implementation of the annotation javax.xml.bind.annotation.XmlElement. However the one in the JDK only applies to Method and Field.

I've found jaxb-api.jar version 2.2.3 permits this on Parameter too, so I want to use this version.

Problem is, I can't figure out how to get maven to use this one in preference to the one in the JDK so that when I am writing my code it doesn't complain that the annotation is being used in an invalid location.

Any suggestions?

回答1:

Include an explicit dependency in your POM:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.2.3</version>
</dependency>


回答2:

You have to use the Java endorsed override mechanism. I got this directly from the Apache CXF website.

JAXB is the default data binding for CXF. If you don't specify one of the other data bindings in your Spring configuration or through the API, you will get JAXB. Releases of CXF since 2.3.x have used the JDK7 default of JAXB 2.2, however Maven users running on JDK 6 will need to use the Java endorsed override mechanism to use JAXB 2.2 instead of JAXB 2.1.

http://cxf.apache.org/docs/jaxb.html first paragraph at the top. You basically download the new version of jaxb then tell the JRE/JDK to load that instead of the default implementation that it shipped with.