What happened to JAXB's NamespacePrefixMapper

2019-01-17 10:33发布

I've been using com.sun.xml.bind.marshaller.NamespacePrefixMapper in my project, and i had no problem with it in JDK 6u17. Now I just updated to 6u18, and I saw that it has been replaced to com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper. However if I import this class and try to compile my classes, I get the error:

package com.sun.xml.internal.bind.marshaller does not exist
import com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper;

I can access this package through the NetBeans code completion feature, and NetBeans does not highlight the code for errors.

Any help would be appreciated!

标签: java jaxb
7条回答
2楼-- · 2019-01-17 11:05

I don't think that the class com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper is a replacement of com.sun.xml.bind.marshaller.NamespacePrefixMapper, the former is there for a long time and it NOT MEANT TO BE USED BY YOU AT ALL (hence the internal packaging).

The problem here is that JavaSE 6 doesn't have the JAXB RI (it has a JAXB implemenation but not JAXB RI) so if you want to rely on RI specific feature, you should bundle JAXB RI in your application (and that would protect you from JAXB changes in Java SE).

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-01-17 11:09

You are not supposed to use com.sun.** classes directly. They are deemed to be internal and subject to change without notice. (And look what just happened!!) The fact that the new class has internal in the package name is an even bigger hint!

I strongly suggest that you look for a better way of doing what you are doing ... that doesn't use the com.sun.** classes.

EDIT - hmmm, looks like whoever is responsible for the JAXB RI has broken the Sun rules about package names for that extension! And it is also unfortunate that Sun has not implemented this particular RI extension in JDK 6.0.

查看更多
做自己的国王
4楼-- · 2019-01-17 11:20

I ran into this recently when porting some older code into a new project. The old project compiled just fine using ant, however the new one failed with the error you mention above.

After some digging, I found that the old build.xml file uses a javac compiler option to bypass the restriction above:

<javac srcdir="${srcDir}" destdir="${outputDir}" classpathref="classpath" debug="on">
    <compilerarg value="-XDignore.symbol.file" />
</javac>

After finding it, I searched and found this other stackoverflow question: Using internal sun classes with javac

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-17 11:20

The below post at stack overflow answers the question: Define Spring JAXB namespaces without using NamespacePrefixMapper

Key is to include the rt.jar at build time and remove it from the application after compilation.

查看更多
The star\"
6楼-- · 2019-01-17 11:24

The NamespacePrefixMapper is not usable anymore.

Use the annotations in package-info.java:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://nameSpaceUri"
, xmlns = {
    @XmlNs(prefix = "myPrefix", namespaceURI = "http://nameSpaceUri")
}
, elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)

package my.package.;

This works with the JAXB bundled with JDK7, for other JDK version update JAXB to 2.2.4.

查看更多
Ridiculous、
7楼-- · 2019-01-17 11:24

For those using maven, found including both JAXB-RI and JAXB for java6 via this link worked.

http://mvnrepository.com/artifact/com.googlecode.jaxb-namespaceprefixmapper-interfaces/JAXBNamespacePrefixMapper/2.2.4

查看更多
登录 后发表回答