users@jersey.java.net

[Jersey] Re: Marshal XML using Jackson

From: Jakub Podlesak <jakub.podlesak_at_oracle.com>
Date: Tue, 31 May 2011 15:00:47 +0200

Hi Tristan,

following is a very stupid implementation of such a provider,
you can just try to add com.fasterxml:jackson-xml-databind:0.5.3
dependency to your pom.xml (given that com.sun.jersey:jersey-json:1.7
is already there) and play with that.

The biggest advantage of using this feature is probably the possibility
to get XML out of raw unannotated POJOs, whereas with JAXB you would need
to wrap it using JAXBElement. If you get something useful out of your
experiment,
and you would be willing to contribute that, we could end up adding such
a support
directly to Jersey.

--8<--
@Provider
@Produces(MediaType.APPLICATION_XML)
public class JacksonXMLMsgBodyWriter implements MessageBodyWriter{

     @Override
     public boolean isWriteable(Class type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
         return true;
     }

     @Override
     public long getSize(Object t, Class type, Type genericType,
Annotation[] annotations, MediaType mediaType) {
         return -1;
     }

     @Override
     public void writeTo(Object t, Class type, Type genericType,
Annotation[] annotations, MediaType mediaType, MultivaluedMap
httpHeaders, OutputStream entityStream) throws IOException,
WebApplicationException {
         XmlMapper xmlMapper = new XmlMapper();
         xmlMapper.writeValue(entityStream, t);
     }
}
-->8--

~Jakub


On 05/19/2011 05:39 PM, Pavel Bucek wrote:
> Hello Tristan,
>
> just implement MessageBodyReader/Writer and put @Provider annotation
> on that class. (You probably know that).
>
> User supplied providers have priority over jersey ones, to it should
> be really simple. Do you have some simple testcase which shows that
> your provider is not taken into account? Or can you at least share
> your provider (I don't need methods implementation)?
>
> Thanks,
> Pavel
>
> On 5/19/11 4:23 PM, Tristan Wehrmaker wrote:
>> Hi!
>>
>> It might seem a little silly but I'm looking for a way to use Jackson
>> to (un)marshall xml.
>>
>> With the POJOMappingFeature it's possible to marshal JSON using Jaxb
>> annotations but without using Jaxb itself, which gives me really nice
>> results.
>> So when I found an extension for Jackson, which allows to marshal XML
>> using Jackson (https://github.com/FasterXML/jackson-xml-databind), I
>> thought it would be nice to see what results this would produce and
>> if they would better suit my needs over Jaxb.
>>
>> My question is now, what is the best way to write a provider for
>> application/xml thats using Jacksons XML databinding. In my attempts
>> my provider get's ignored and the Jaxb provider is used.
>>
>> Regards
>> Tristan
>>
>
>