MOXy's @XmlCDATA 似乎没有影响 [英] MOXy's @XmlCDATA seems to have no affect

查看:72
本文介绍了MOXy's @XmlCDATA 似乎没有影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望将以下内容返回给浏览器(查看源代码)

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>

如果,我将 content 的值更改为

If, I change the value of content to be

< ;![CDATA[请在未转义的 CDATA 标签中显示此内容]]>,标签的小于和大于被转义.

&lt ;![CDATA[Please show this inside a unescaped CDATA tag]]&gt ; , 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.

元数据

您拥有的 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.

我的对象

默认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>

了解更多信息

这篇关于MOXy&amp;#39;s @XmlCDATA 似乎没有影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆