users@jersey.java.net

JSONJAXBContextResolver question...

From: Rabick, Mark A (IS) <"Rabick,>
Date: Tue, 12 May 2009 13:07:48 -0500

My app is 'working' while I expect it not to be... I have a class that extends ContextResolver that lists the 'types' included in the JSONJAXBContext and for which the context should be returned:

/**
 * A JSON/JAXB context resolver for marshalling and unmarshalling objects
 * in XML or JSON.
 */
@Provider
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public final class JSONJAXBContextResolver implements ContextResolver<JAXBContext> {
    
    private final JAXBContext context;
    
    private final Set<Class> types;
    
    /*
     * The list of JAXB class types (top-level) need to be listed here.
     */
    private final Class[] cTypes = {NodeV1.class, EntityAttributeMapV1.class,
                                                                    java.util.ArrayList.class};
    
    /**
     * Constructor that initializes the set of classes this provider will
     * resolve for JAXB and JSON. The JSON configuration is set to the
     * 'natural' formatting.
     * @throws Exception On initialization error.
     */
    public JSONJAXBContextResolver() throws Exception {

            this.types = new HashSet(Arrays.asList(cTypes));
        this.context = new JSONJAXBContext(
                        JSONConfiguration.natural().build(), cTypes);
    }
    
    /**
     * @see javax.ws.rs.ext.ContextResolver#getContext(java.lang.Class)
     */
    public JAXBContext getContext(Class<?> objectType) {
        return (types.contains(objectType)) ? context : null;
    }
}

If I call a resource that marshalls a class not in the Class[] cTypes array above, it still marshalls the entities as expected? I've noticed this since I've gone to Jersey 1.0.3.

--mark

_______________________________________________
Mark A. Rabick
Software Engineer
Northrop Grumman - Integrated Mission Systems (IS/DSD/IMS)
3200 Samson Way
Bellevue, NE 68123
Ph: (402) 293-7091
Em: mark.rabick_at_ngc.com
Remember PFC Ross A. McGinnis...
  http://www.army.mil/medalofhonor/McGinnis/index.html
... MA2 Michael A. Monsoor, Lt. Michael P. Murphy, Cpl. Jason Dunham, SFC Paul Ray Smith and the rest...
  http://www.cmohs.org/recipients/most_recent.htm