users@jersey.java.net

Jaxb cdata

From: saturon <netbeans_at_vtxfree.ch>
Date: Thu, 25 Jun 2009 18:36:57 +0200

Hi there,

I now come to a problem i already had 1.5 years back and then solved
with a workaround. Now after 1.5 years I wonder if there are any better
solutions.

My stack:
Jersey 1.01/Glassfish 2.1

To develop: NB 6.1 with jersey plugin

REST services serve xml, which partly contains xml-illegal characters.
Therefore I need to produce xml elemements with cdata tags.
I have jaxb annotated converters that wrap my entity beans.

So how do you do that best?

My solution 1.5 years back was to write MessageBodyWriter for every
Converter involved. In the MessageBodyWriter I configured the serializer
with the specifc cdata OutputFormat, actually using the depreciated
org.apache.xml.serialize.XMLSerializer and
org.apache.xml.serialize.OutputFormat.
Something like:

@Produces("application/xml")
@Provider
public class MyConverterWriter implements MessageBodyWriter<MyConverter>
{
..
private XMLSerializer getXMLSerializer(OutputStream out) {
        // configure an OutputFormat to handle CDATA
        OutputFormat of = new OutputFormat();

        of.setCDataElements(new String[]{"^desc", "^log"});
       
        XMLSerializer serializer = new XMLSerializer(of);
        serializer.setOutputByteStream(out);

        return serializer;
    }
..


As you see not very elegant, verbose ( I have now something like 35
Converters which I would need to write MessageBodyWriter for .,), and
also error prone to forget things.

Is there any better solution for this problem?
Can't be that I am the only one and that JAXB did not foresee such a
thing?


Thanks, Ben