Spring3、JAXB2、Java6、NamespacePrefixMapper 问题

发布于 2024-10-10 12:23:58 字数 822 浏览 6 评论 0原文

我构建了一个简单的 Spring3、Hibernate3/(JPA2)、RESTful 服务,托管在 Tomcat6 上,它使用 JAXB2 来编组结果。 (它使用带注释的 pojo。)我需要使用特定的命名空间前缀,因此我编写了一个自定义 com.sun.xml.bind.marshaller.NamespacePrefixMapper。我将 JAXB2 RI jar 包含在我的应用程序中,一切正常。

然后有人说那太好了,我们也需要将其托管在 WebLogic 11g (10.3.3) 下。没问题,我创建了特殊的 weblogic 部署描述符来更喜欢应用程序 jar,将其重命名为 persistence.xml,并使用 JPA2 jar 将 WAR 包装在 EAR 中。效果很好,几乎。

不幸的是,我们的 WebLogic 服务器运行一个也使用 JAXB 的自定义安全领域,并导致与我的应用程序发生冲突。因此,我从应用程序中删除了 JAXB jar,它在 WebLogic 中运行良好。当然,除非我将 JAXB jar 添加到 Tomcat,否则它不再在 Tomcat 下运行。我想避免这种情况。

所以我的问题...我在 stackoverflow 上读过很多帖子,其中包含很多关于使用 sun“内部”​​JAXB2 实现与将 RI 与应用程序打包相关的意见/分歧。这个问题还没有一个干净的解决方案吗?我的堆栈是否支持另一种方式来自定义映射我的名称空间前缀而不包含 JAXB2 RI?我可以安全地使用 Java6“内部”JAXB NamespacePrefixMapper,还是会随着各种 Java 版本的出现而消失? Spring3 是否提供了另一种解决方案? Java6 JAXB2 实现的真实情况是什么?它仅供Sun(Oracle)内部使用吗?

谢谢。

I built a simple Spring3, Hibernate3/(JPA2), RESTful service, hosted on Tomcat6, that uses JAXB2 to marshal the results. (It uses annotated pojos.) I needed to use specific namespace prefixes, so I wrote a custom com.sun.xml.bind.marshaller.NamespacePrefixMapper. I included the JAXB2 RI jars with my application and everything worked fine.

Then someone said that's great, we need to host it under WebLogic 11g (10.3.3) too. No problem, I created the special weblogic deployment descriptors to prefer the application jars, renamed my persistence.xml, and wrapped the WAR in an EAR with the JPA2 jars. It worked great, almost.

Unfortunately, our WebLogic server runs a custom security realm that also uses JAXB and causes conflicts with my application. So I dropped the JAXB jars from the app and it runs fine in WebLogic. Of course it no longer runs under Tomcat unless I add the JAXB jars to Tomcat. I'd like to avoid that.

So my questions... I've read quite a few posts on stackoverflow that contain a lot of opinions/disagreements regarding the use of the sun "internal" JAXB2 implementation vs. packaging the RI with your app. Is there not yet a clean solution to this problem? Does my stack support another way to custom map my namespace prefixes without including the JAXB2 RI? Can I safely use the Java6 "internal" JAXB NamespacePrefixMapper, or will that come and go with various Java releases? Does Spring3 offer another solution? What's the true story on the Java6 JAXB2 implementation? Is it only there for Sun's (Oracle's) internal use?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

聚集的泪 2024-10-17 12:23:58

正如评论中提到的,我将总结 http://www.func.nl/community/knowledgebase/customize-namespace-prefix-when-marshalling-jaxb

注意:我自己没有尝试过,所以它可能不起作用。

本质上,您将 JAXB 编组器配置为在编组时使用 XMLStreamWriter,并将其配置为映射前缀,例如,

XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
xmlStreamWriter.setPrefix("func", "http://www.func.nl");

JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();

marshaller.marshal(object, xmlStreamWriter);

想法是,如果 JAXB 尚未获得前缀映射器,那么它将离开它由 XMLStreamWriter 来处理前缀,通过执行上述操作,您可以告诉它如何执行此操作。

再说一次:我只是重复您的网络被屏蔽的网站上的内容,所以我不认为它是正确的,也不指责它是错误的。

As mentioned in the comments, I'll summarise what is mentioned in http://www.func.nl/community/knowledgebase/customize-namespace-prefix-when-marshalling-jaxb.

Note: I haven't tried this myself, so it may not work.

Essentially, you configure the JAXB marshaller to use an XMLStreamWriter when marshalling, and you configure that to map prefixes, e.g.

XMLStreamWriter xmlStreamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
xmlStreamWriter.setPrefix("func", "http://www.func.nl");

JAXBContext context = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = context.createMarshaller();

marshaller.marshal(object, xmlStreamWriter);

The idea is that if JAXB hasn't been given a prefix mapper, then it'll leave it up to the XMLStreamWriter to handle the prefixes, and by doing the above, you're telling it how to do it.

Again: I'm just repeating the content from the website that's blocked from your network, so I take no credit for it being right, and no blame for it being wrong.

亢潮 2024-10-17 12:23:58

The EclipseLink JAXB (MOXy) will use the namespace prefixes as declared in the @XmlSchema annotation.

For more information see:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文