JAXB 中的 XML 字符串

发布于 2024-09-03 14:57:21 字数 637 浏览 1 评论 0原文

我有一个模仿表的 JPA 实体类。像这样的事情:

@XmlType(name="MyClassElementType")
public class MyClass {
    String name;
    String xmlDesc;

    public MyClass() {}

    @XmlElement
    String getName() { return name; }
    void setName(String name) { this.name = name; }

    @XmlElement
    String getXmlDesc() { return xmlDesc; }
    void setXmlDesc(String xmlDesc) { this.xmlDesc = xmlDesc; }
}

在 Jersey REST get 调用中,我试图返回此类:

@Get
@Produces("application/xml")
public MyClass get() {

    return myClass;
}

现在我期望已存在的 xml 字符串(xmlStr)按原样返回,但 Jersey/JAXB 转义了它......

所以无论如何?

I have a JPA entity class mimicking a table. Something like this:

@XmlType(name="MyClassElementType")
public class MyClass {
    String name;
    String xmlDesc;

    public MyClass() {}

    @XmlElement
    String getName() { return name; }
    void setName(String name) { this.name = name; }

    @XmlElement
    String getXmlDesc() { return xmlDesc; }
    void setXmlDesc(String xmlDesc) { this.xmlDesc = xmlDesc; }
}

In a Jersey REST get call I'm trying to return this class:

@Get
@Produces("application/xml")
public MyClass get() {

    return myClass;
}

Now I'm expecting the already xml string(xmlStr) to be returned as is, but Jersey/JAXB escapes it...

So anyway around this?

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

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

发布评论

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

评论(1

救赎№ 2024-09-10 14:57:21

JAXB 无法知道 xmlDesc 包含 XML 字符串,它可以是任何内容,因此它必须对其进行转义。

如果要在 JAXB 对象模型中存储任意 XML,则需要将其存储为 org.w3c.dom.Element 的实例。然后,JAXB 应根据需要将其与 XML 进行转换。

JAXB has no way of knowing that xmlDesc contains an XML string, it could be anything, so it has to escape it.

If you want to store arbitrary XML in a JAXB object model, you need to store it as an instance of org.w3c.dom.Element. JAXB should then convert that to/from XML as necessary.

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