MOXy's @XmlCDATA seems to have no affect

佐手、 提交于 2019-12-05 14:07:56

CLASSPATH

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

The EclipseLink JAXB (MOXy) metadata you have provided appears to be correct. This can be verified with the following standalone demo code.

MyObject

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

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

Demo

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);
    }

}

Output

<?xml version="1.0" encoding="UTF-8"?>
<myObject>
   <content><![CDATA[Please show this inside a unescaped CDATA tag]]></content>
</myObject>

For More Information

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!