users@jersey.java.net

Re: [Jersey] Jaxb cdata

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 25 Jun 2009 18:49:19 +0200

Hi,

It is not currently possible to support this with the existing JAXB
providers i.e. it is not possible to define a content handler that the
JAXB beans are serialized to, or to declare the CDATA with the JAXB
bean.

But it looks like you are defining your own JAXB supported providers.
So... why don't you define that each convertor implement a common
interface or is annotated with a common annotation. Then write one
MessageBodyWriter supporting that interface or annotation. The CDATA
mapping can be defined by the common interface or the annotation.

Paul.

On Jun 25, 2009, at 6:36 PM, saturon wrote:

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