users@jersey.java.net

[Jersey] Re: how to rename XmlRootElement in JSON

From: Maxrunner <joao.rossa_at_gmail.com>
Date: Mon, 30 May 2011 11:38:42 -0700 (PDT)

Heres my context resolver:

@Component
@Provider
public class JacksonObjectMapperProvider extends
JacksonAnnotationIntrospector implements ContextResolver<ObjectMapper> {
 //object mappers to be used for configurations
private ObjectMapper defaultObjectMapper;
private ObjectMapper listsObjectMapper;

     public JacksonObjectMapperProvider() {
super();
 MyObjectMapperProvider();
}

public void MyObjectMapperProvider() {
 //AnnotationIntrospector primary = new JacksonAnnotationIntrospector();
AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();
AnnotationIntrospector pair = new AnnotationIntrospector.Pair(this,
secondary);

//object provider for default objects - show root name
    defaultObjectMapper = new ObjectMapper();
    defaultObjectMapper.configure(Feature.INDENT_OUTPUT, true);
    defaultObjectMapper.configure(Feature.WRAP_ROOT_VALUE, true);

    // make deserializer use JAXB annotations

defaultObjectMapper.getDeserializationConfig().setAnnotationIntrospector(pair);
 // make serializer use JAXB annotations

defaultObjectMapper.getSerializationConfig().setAnnotationIntrospector(pair);


    //object provider for list type objects - do not show root name
    listsObjectMapper= new ObjectMapper();
    listsObjectMapper.configure(Feature.INDENT_OUTPUT, true);
    listsObjectMapper.configure(Feature.WRAP_ROOT_VALUE, false);

    // make deserializer use JAXB annotations

listsObjectMapper.getDeserializationConfig().setAnnotationIntrospector(pair);
 // make serializer use JAXB annotations

listsObjectMapper.getSerializationConfig().setAnnotationIntrospector(pair);
    }

 @Override
public String findRootName(AnnotatedClass ac) {
 return super.findRootName(ac);
}
 public ObjectMapper getContext(Class<?> type) {
   if (type == ListWrapperWithMap.class) {
             return listsObjectMapper;
         } else {
             return defaultObjectMapper;
         }
 }
}

I only need to do the logic there(findRootName), you're saying?

regards,

2011/5/30 João Rossa <joao.rossa_at_gmail.com>

> *"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.
>
> *"(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?, i dont really understand how to use mix in annotations, the jackson
> documentation isnt really that explicit,
>
> *"(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,
>
>
> On Mon, May 30, 2011 at 7:14 PM, Cowtowncoder [via Jersey] <
> ml-node+6420107-733476264-73314_at_n2.nabble.com> wrote:
>
>> On Mon, May 30, 2011 at 6:24 AM, Maxrunner <[hidden email]<http://user/SendEmail.jtp?type=node&node=6420107&i=0>>
>> wrote:
>> > Well that's only a way to solve my issue. I just want tho change the
>> List
>> > type name to one my own, like in here:
>>
>> 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.
>>
>> To get wrapping you want, you have couple of choices:
>>
>> (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.
>> (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.
>>
>> I suggest you try out one of these approaches as they would solve your
>> problem, as long as correct ObjectMapper is being used.
>>
>> -+ Tatu +-
>>
>>
>> ------------------------------
>> If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6420107.html
>> To unsubscribe from how to rename XmlRootElement in JSON, click here<http://jersey.576304.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=6173292&code=am9hby5yb3NzYUBnbWFpbC5jb218NjE3MzI5MnwyMDYzODA1MDkw>.
>>
>>
>
>


--
View this message in context: http://jersey.576304.n2.nabble.com/how-to-rename-XmlRootElement-in-JSON-tp6173292p6420169.html
Sent from the Jersey mailing list archive at Nabble.com.