Java JDK6u18 中 JAXB 的 NamespacePrefixMapper 发生了什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2326107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:10:34  来源:igfitidea点击:

What happened to JAXB's NamespacePrefixMapper in JDK6u18

javajaxb

提问by Daniel Szalay

I've been using com.sun.xml.bind.marshaller.NamespacePrefixMapperin 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:

我一直com.sun.xml.bind.marshaller.NamespacePrefixMapper在我的项目中使用,我在JDK 6u17中没有问题。现在刚更新到6u18,看到已经换成com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper. 但是,如果我导入此类并尝试编译我的类,则会出现错误:

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.

我可以通过 NetBeans 代码完成功能访问这个包,而且 NetBeans 不会突出显示错误的代码。

Any help would be appreciated!

任何帮助,将不胜感激!

采纳答案by Pascal Thivent

I don't think that the class com.sun.xml.internal.bind.marshaller.NamespacePrefixMapperis 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 internalpackaging).

我不认为该类com.sun.xml.internal.bind.marshaller.NamespacePrefixMapper是 的替代品com.sun.xml.bind.marshaller.NamespacePrefixMapper,前者存在很长时间,并且根本不打算由您使用(因此是internal包装)。

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).

这里的问题是 JavaSE 6 没有 JAXB RI(它有 JAXB 实现但没有 JAXB RI)所以如果你想依赖 RI 特定的特性,你应该在你的应用程序中捆绑 JAXB RI(这会保护你来自 Java SE 中的 JAXB 更改)。

回答by Stephen C

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 internalin the package name is an even bigger hint!

您不应该com.sun.**直接使用类。它们被视为内部的,如有更改,恕不另行通知。(看看刚刚发生了什么!!)internal包名中包含新类的事实是一个更大的提示!

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

我强烈建议你寻找一种更好的方式来做你正在做的事情......不使用com.sun.**类。

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.

编辑- 嗯,看起来负责 JAXB RI 的人违反了 Sun 关于该扩展包名称的规则!而且很遗憾,Sun 没有在 JDK 6.0 中实现这个特殊的 RI 扩展。

回答by Bozho

Sun had made something not quite appropriate in this case. The namespace mapper isn't included in the spec, but it is "advertised" as a way to customize prefixes. So the general advice "don't use com.sun.*" doesn't apply here, and the javadocof this class says:

在这种情况下,Sun 做了一些不太合适的事情。命名空间映射器未包含在规范中,但它被“宣传”为自定义前缀的一种方式。所以一般的建议“不要使用com.sun.*”在这里不适用,这个类的javadoc说:

Implemented by the user application to determine URI -> prefix mapping.

由用户应用程序实现以确定 URI -> 前缀映射。

Check this articleand see if it would work for you.

检查这篇文章,看看它是否适合你。

回答by Daniel De León

The NamespacePrefixMapperis not usable anymore.

NamespacePrefixMapper不再是不可用的。

Use the annotations in package-info.java:

使用中的注释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.

这适用于与 JDK7 捆绑的 JAXB,对于其他 JDK 版本,将 JAXB 更新为 2.2.4。

回答by Travis

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.

我最近在将一些旧代码移植到新项目中时遇到了这个问题。旧项目使用 ant 编译得很好,但是新项目因您上面提到的错误而失败。

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

经过一番挖掘,我发现旧的 build.xml 文件使用 javac 编译器选项来绕过上述限制:

<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

找到后,我搜索并发现了另一个stackoverflow问题: Using internal sun classes with javac

回答by Steve

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

对于那些使用 maven 的人,发现通过此链接包括 JAXB-RI 和 JAXB for java6 都有效。

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

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

回答by Mr. Doomsbuster

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

下面堆栈溢出的帖子回答了这个问题: 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.

关键是在构建时包含 rt.jar 并在编译后将其从应用程序中删除。