Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing MOXy's @XmlCDATA annotation with jersey
    text
    copied!<p>I'm working on REST web-service written with jersey and I'm trying to output some XML with CDATA sections in it. I understand <a href="https://stackoverflow.com/questions/3136375/how-to-generate-cdata-block-using-jaxb/3153660#3153660">the reference implementation of JAXB doesn't support that</a>, so I've downloaded EclipseLink's MOXy and I'm trying to get the @XmlCDATA annotation to work.</p> <p>My JAXB mapped bean looks like this</p> <pre><code>package com.me.entities; @XmlRootElement @XmlAccessorType(XmlAccessType.FIELD) public class MyBean { @XmlAttribute private URI thumbnail; @XmlElement(name="longdescription") @XmlCDATA private String description; public MyBean() { } public final String getDescription() { return description; } public final void setDescription(String d) { this.description = d; } } </code></pre> <p>and I have the <code>jaxb.properties</code> file in the <code>com/me/entities</code> along with the class files. The properties file has </p> <pre><code>javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory </code></pre> <p>in it. I'm sure it gets loaded successfully since if I replace the factory class name with some nonsense the app breaks down. Also, explicitly marshaling the file creating the JAXBContext on my own works fine, so the problem seems related to jersey. According to <a href="http://bdoughan.blogspot.com/2010/07/cdata-cdata-run-run-data-run.html" rel="nofollow noreferrer">this</a> my setup is fine, but when my jersey resource returns an instance of MyBean </p> <pre><code>... @GET @Produces(MediaType.TEXT_XML) public MyBean getMyBean() { MyBean b = new MyBean(); b.setDescription("Some blurb plenty of invalid chars like &lt;&lt;&lt; &amp;&amp; &gt;&gt;&gt;"); return b; } </code></pre> <p>what I get back has no CDATA in it, but looks like </p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;info&gt;&lt;longdescription&gt;Some blurb plenty of invalid chars like &amp;lt;&amp;lt;&amp;lt; &amp;amp;&amp;amp; &gt;&gt;&gt;&lt;/longdescription&gt;&lt;/info&gt; </code></pre> <p>What am I doing wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload