users@jersey.java.net

Glassfish, Jersey and JSON

From: John Lister <john.lister_at_kickstone.com>
Date: Sun, 28 Feb 2010 14:52:50 +0000

Hi, using glassfish V3 and Jersey 1.4.1 and I'm playing with the JSON
support which is where I've got 2 questions:
I'm trying to use the natural notation and struggling to get it to work,
I've followed the many examples on the web and created a Provider class
that implements ContextResolver as follows:

@Provider
   public class MyJAXBContextResolver implements ContextResolver<JAXBContext> {

       private JAXBContext context;
       private Class[] types = {MyBean.1class, MyBean2.class};

       public MyJAXBContextResolver() throws Exception {
           this.context = new JSONJAXBContext(
                   JSONConfiguration.mapped()
                                      .rootUnwrapping(true)
                                      .arrays("arrayVar")
                                      .nonStrings("number1", "number2")
                                      .build(),
                   types);
       }

       public JAXBContext getContext(Class<?> objectType) {
           return (types[0].equals(objectType)) ? context : null;
       }
   }


but it doesn't get picked up by glassfish, The logs just show
INFO: Providers classes found:
after my resources are found which concludes me to believe the provider
class hasn't been picked up.
Have missed something somewhere or does this not work with glassfish...


Secondly is it possible through some form of configuration to specify
the natural notation as the default or at least set it for all classes,
at the minute it seems I have to list all classes I want it to be
applied to (which is every class in my project) which has the potential
to cause problems in the future if I forget to update the list when a
new data class is added.
 

Thanks