JAXB 中的 XML 字符串
我有一个模仿表的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.