Jersey 2.6 Jackson provider registering

Asela Senanayake picture Asela Senanayake · Jan 14, 2015 · Viewed 18.4k times · Source

I'm implementing REST web service with Jersey 2.6,

I'm having troubles of registering a Jackson Provider for JSON support, I have implemented according to the jeresy documentation (https://jersey.java.net/documentation/2.6/user-guide.html#json.jackson).

  1. Add maven dependency - jersey-media-json-jackson
  2. Implemented a ContextResolver class.
  3. Annotated it with @Provider to enable "Auto-Discoverable Features"
  4. web.xml has the package name of the provider classes, so that Providers will be registered during the scan.

ref: http://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/

For some reason Jackson JSON provider is not registered, am I missing something?

Answer

Paul Samsotha picture Paul Samsotha · Jan 14, 2015

Up until Jersey 2.9, that feature is not auto-discovered. We need to either (1) explicitly register the JacksonFeature in the Application/ResourceConfig subclass, (2) list the Jackson package in the web.xml of packages to scan, or (3) add the JacksonFeature to the list of providers in the web.xml

Application subclass:

public class MyApplication extends ResourceConfig {  
    public MyApplication() {
        // (1)
        register(JacksonFeature.class); // <----- Jackson Support
        packages("the.package.of.your.resources");
    }
}

Or web.xml:

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>
        org.glassfish.jersey.servlet.ServletContainer
    </servlet-class>
    <init-param>
        <!-- (2) -->
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>
            the.package.of.your.resources,
            org.codehaus.jackson.jaxrs <!-- Jackson providers -->
        </param-value>
    </init-param>
    <init-param>
        <!-- (3) -->
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>
            org.glassfish.jersey.jackson.JacksonFeature
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

See more details here in the "Second Issue". Note that for the ...classnames property, if you have more than one provider to register, you should list it in the same param-value delimited with a comma, semicolon, or newline.

Oh and just an FYI, the ContextResolver is only to register the ObjectMapper in a retrievable context, so the MessageBodyReader/MessageBodyWriters can reuse it. But it does not register the actual MessageBodyReader/Writer that is required for the marshalling/unmarshalling.