MOXy的@XmlCDATA似乎没有任何影响 [英] MOXy's @XmlCDATA seems to have no affect
问题描述
我想将以下内容返回浏览器(查看源代码)
I would like to have the following returned to the browser (view source)
<content>
<![CDATA[Please show this inside a unescaped CDATA tag]]>
</content>
但我实际上得到了
<content>
Please show this inside a unescaped CDATA tag
</content>
如果,我将内容的值更改为
If, I change the value of content to be
& lt;![CDATA [请在非转义的CDATA标签内显示]]& gt;
,标签的小于和大于转义。
< ;![CDATA[Please show this inside a unescaped CDATA tag]]> ; , the less than and the greater than for the tag are escaped.
想知道如何实现我想要的东西????
Wondering how to achieve what I wanted????
这是我的代码
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/myRequest")
public class MyRestClass {
@GET
@Path("{myPathNumber}")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Object doInquiry(@PathParam("myPathNumber") String myPathNumber) {
try {
return new MyObject();
} catch (Exception e) {
return "exception " + e.getMessage();
}
}
}
package org.openengine.wink;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.eclipse.persistence.oxm.annotations.XmlCDATA;
@XmlRootElement
public class MyObject implements Serializable {
@XmlElement
@XmlCDATA
private String content = "Please show this inside a unescaped CDATA tag";
}
包 org.openengine。 wink
我有一个文件, jaxb.properties
,包含以下内容
in package org.openengine.wink
I have a file, jaxb.properties
, with the following content
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
推荐答案
CLASSPATH
我最好的猜测是 EclipseLink JAXB(MOXy) 未在类路径上正确配置,而JAXB RI是在您的环境中用作JAXB(JSR-222)提供程序。
My best guess is that EclipseLink JAXB (MOXy) is not correctly configured on your classpath, and the JAXB RI is being used as the JAXB (JSR-222) provider in your environment.
METADATA
EclipseLink JAXB(MOXy) 您提供的元数据似乎是正确的。这可以使用以下独立演示代码进行验证。
The EclipseLink JAXB (MOXy) metadata you have provided appears to be correct. This can be verified with the following standalone demo code.
MyObject
按默认 JAXB(JSR-222) 实施查找属性上的元数据(getter / setter)。由于您已注释该字段,我建议使用 @XmlAccessorType(XmlAccessType.FIELD
注释(请参阅: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html 。
By default JAXB (JSR-222) implementations look for metadata on the property (getter/setter). Since you have annotated the field I would recommend using the @XmlAccessorType(XmlAccessType.FIELD
annotation (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).
package org.openengine.wink;
import java.io.Serializable;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlCDATA;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class MyObject implements Serializable {
@XmlElement
@XmlCDATA
private String content = "Please show this inside a unescaped CDATA tag";
}
jaxb.properties
要将MOXy指定为JAXB提供程序,您需要在类路径上安装EclipseLink二进制文件并拥有一个文件在与您的域模型相同的包中调用 jaxb.properties
,并带有以下条目(请参阅: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html )。
To specify MOXy as your JAXB provider you need to have the EclipseLink binaries on your classpath and have a file called jaxb.properties
in the same package as your domain model with the following entry (see: http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html).
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示
package org.openengine.wink;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(MyObject.class);
MyObject myObject = new MyObject();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(myObject, System.out);
}
}
输出
<?xml version="1.0" encoding="UTF-8"?>
<myObject>
<content><![CDATA[Please show this inside a unescaped CDATA tag]]></content>
</myObject>
更多信息
- http://blog.bdoughan.com/2010/07/cdata-cdata-run-run-data-run.html
这篇关于MOXy的@XmlCDATA似乎没有任何影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!