将 MOXy 的 @XmlCDATA 注释与球衣一起使用

发布于 2024-11-26 13:21:13 字数 1674 浏览 2 评论 0原文

我正在开发用 jersey 编写的 REST Web 服务,并且尝试输出一些带有 CDATA 部分的 XML。我了解 JAXB 的参考实现不支持该功能,所以我下载了 EclipseLink 的 MOXy,并尝试让 @XmlCDATA 注释起作用。

我的 JAXB 映射 bean 如下所示

package com.me.entities;

@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD)
public class MyBean {

    @XmlAttribute
    private URI thumbnail;

    @XmlElement(name="longdescription") @XmlCDATA
    private String description;

    public MyBean() { }

    public final String getDescription() { return description; }

    public final void setDescription(String d) { this.description = d; }
}

,并且我在 com/me/entities 中有 jaxb.properties 文件以及类文件。属性文件包含

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

在其中。我确信它会成功加载,因为如果我用一些废话替换工厂类名称,应用程序就会崩溃。另外,显式编组自己创建 JAXBContext 的文件效果很好,因此问题似乎与球衣有关。根据 this 我的设置是很好,但是当我的球衣资源返回 MyBean 的实例时,

...
@GET @Produces(MediaType.TEXT_XML) 
public MyBean getMyBean() {
    MyBean b = new MyBean();
    b.setDescription("Some blurb plenty of invalid chars like <<< && >>>");

    return b;
 }

我返回的内容中没有 CDATA,但看起来像

<?xml version="1.0" encoding="UTF-8"?>
<info><longdescription>Some blurb plenty of invalid chars like &lt;&lt;&lt; &amp;&amp; >>></longdescription></info>

我做错了什么?

I'm working on REST web-service written with jersey and I'm trying to output some XML with CDATA sections in it. I understand the reference implementation of JAXB doesn't support that, so I've downloaded EclipseLink's MOXy and I'm trying to get the @XmlCDATA annotation to work.

My JAXB mapped bean looks like this

package com.me.entities;

@XmlRootElement @XmlAccessorType(XmlAccessType.FIELD)
public class MyBean {

    @XmlAttribute
    private URI thumbnail;

    @XmlElement(name="longdescription") @XmlCDATA
    private String description;

    public MyBean() { }

    public final String getDescription() { return description; }

    public final void setDescription(String d) { this.description = d; }
}

and I have the jaxb.properties file in the com/me/entities along with the class files. The properties file has

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

in it. I'm sure it gets loaded successfully since if I replace the factory class name with some nonsense the app breaks down. Also, explicitly marshaling the file creating the JAXBContext on my own works fine, so the problem seems related to jersey. According to this my setup is fine, but when my jersey resource returns an instance of MyBean

...
@GET @Produces(MediaType.TEXT_XML) 
public MyBean getMyBean() {
    MyBean b = new MyBean();
    b.setDescription("Some blurb plenty of invalid chars like <<< && >>>");

    return b;
 }

what I get back has no CDATA in it, but looks like

<?xml version="1.0" encoding="UTF-8"?>
<info><longdescription>Some blurb plenty of invalid chars like <<< && >>></longdescription></info>

What am I doing wrong?

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

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

发布评论

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

评论(2

从此见与不见 2024-12-03 13:21:13

看起来问题出在我的应用程序服务器上:我在开发模式下使用 WebLogic 10.3.5 运行它,它预装了许多常用库,在默认配置中它们优先于部署的库在 webapp WEB-INF/lib 文件夹中。

要解决此问题,需要特定于 weblogic 的应用程序描述,只需在包含 prefer-web-inf-classesWEB-INF 内创建一个 weblogic.xml 文件即可代码>选项。我使用的文件是这样的:

<?xml version='1.0' standalone='yes'?>
<weblogic-web-app>
  <container-descriptor>
    <prefer-web-inf-classes>true</prefer-web-inf-classes>
  </container-descriptor>
</weblogic-web-app>

我仍然不知道哪个库是问题所在,任何人都知道可以随意编辑这个答案。

Looks like the problem was my application server: I am running this with WebLogic 10.3.5 in development mode, which comes with a lot of common libraries pre-installed that in the default configuration take precedence over those deployed in the webapp WEB-INF/lib folder.

To fix this a weblogic specific application description is needed, just create a weblogic.xml file inside WEB-INF containing the prefer-web-inf-classes option. The file I used is this:

<?xml version='1.0' standalone='yes'?>
<weblogic-web-app>
  <container-descriptor>
    <prefer-web-inf-classes>true</prefer-web-inf-classes>
  </container-descriptor>
</weblogic-web-app>

I still have no idea which library was the problem though, anyone knows feel free to edit this answer.

俯瞰星空 2024-12-03 13:21:13

请下载 Jaxb 扩展:
这是 Jaxb 的 Eclipselink 开源扩展。
获取jar文件:将eclipselink.jar复制到Project lib中。

http://www.eclipse.org/eclipselink/downloads/
EclipseLink 2.4.1 Installer Zip (37 MB)

并参见示例:

http://theopentutorials.com/tutorials/java/jaxb/jaxb-marshalling-and-unmarshalling-cdata-block-using-eclipselink-moxy/

好看!

Please download Jaxb Extension:
This is Eclipselink open source extension for Jaxb.
Get jar file: eclipselink.jar copy into Project lib.

http://www.eclipse.org/eclipselink/downloads/
EclipseLink 2.4.1 Installer Zip (37 MB)

And see example at:

http://theopentutorials.com/tutorials/java/jaxb/jaxb-marshalling-and-unmarshalling-cdata-block-using-eclipselink-moxy/

Good look!.

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