users@jersey.java.net

[Jersey] Re: how to rename XmlRootElement in JSON

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Tue, 31 May 2011 14:59:56 -0700

On Mon, May 30, 2011 at 11:33 AM, Maxrunner <joao.rossa_at_gmail.com> wrote:
> "I have mentioned this multiple times, but wrapping only applies to the
> actual type of the value. In this case it is the List type; and
> annotation thereby MUST BE for that type.
> Whatever types of values within List have is irrelevant and will not
> be used under any circumstances. "
> If you're using a list wrapper class then the name of the variable
> containing the list is the one used. But then it also adds the wrapper class
> itself.

No. If you are already using a list and just switch to using different
kind of list, nothing changes except for wrapper name being used. I am
not suggesting adding of new wrapper objects or list, but of using
different List implementation.

> "(a) Add @XmlRootElement annotation to relevant List type; either by
> sub-classing as a new class with annotation; or by using mix-in
> annotations to associate annotation. "
> The fix jakub is doing seems to be the more straightforward option. That way
> i can hide the root element according to the object mapper. Dont you agree?,

While it is a useful fix and allows different kind of wrapping, it
does not change the procedure by which name of the wrapper is chosen.

> i dont really understand how to use mix in annotations, the jackson
> documentation isnt really that explicit,

This page: http://wiki.fasterxml.com/JacksonMixInAnnotations
explains some aspects, and this blog entry
http://www.cowtowncoder.com/blog/archives/2009/08/entry_305.html
more. But you do not have to use mix-ins if you can use other mechanisms.

> "(b) Implement custom AnnotationIntrospector (extending either
> JacksonAnnotationIntrospector or JaxbAnnotationIntrospector) and
> override 'findRootName' method; in this case you are free to use
> whatever logic you want to associate wrapper name. "
> where do i do this?in the context resolver class?
> regards,

No, since context resolver is only used by Jersey;
AnnotationIntrospector is what Jackson uses (and Jackson does not
depend on Jersey (or JAX-RS API) for its configuration).
So you need to register it with ObjectMapper; and ObjectMapper is what
is registered with Jersey.

Hope this helps,

-+ Tatu +-