users@jersey.java.net

[Jersey] How to make a JSONConfiguration

From: Pengfei Di <pengfei.di_at_match2blue.com>
Date: Wed, 6 Apr 2011 13:28:19 +0200

Hello Group,

I am trying to turn off the rootUnwrapping feature by specifying a
JSONConfiguration. (In other words: I'd like to add the class name into
the JSON string.)
Although I added a JAXBContextResolver (from Example 5.6 of the Jersey
Guide) into my project, the new JSONConfiguration seems not to take effect.
What I found is that, the constructor method of JAXBContextResolver has
been called once, but the getContext method has never been called, no
matter how many responses are generated.

Shouldn't the getContext method be called?
Did I forget something?

Thanks a lot for any hint.

Pengfei


The source code of the JAXBContextResolver is following:

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {
     private JAXBContext context;
     private Class[] types = { MyClass1.class, MyClass2.class };

     public JAXBContextResolver() throws Exception {
         JSONConfiguration config =
JSONConfiguration.natural().rootUnwrapping(false).build();
         this.context = new JSONJAXBContext(config, this.types);
     }

     @Override
     public JAXBContext getContext(Class<?> objectType) {
         System.out.println(" --------- This sentence never apprears!
----------");
         for (Class<?> type : this.types) {
             if (type == objectType) {
                 return this.context;
             }
         }
         return null;
     }
}