Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey 2.6 Jackson provider registering

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?

like image 774
Asela Senanayake Avatar asked Jan 14 '15 03:01

Asela Senanayake


2 Answers

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.

like image 107
Paul Samsotha Avatar answered Nov 19 '22 07:11

Paul Samsotha


Hi I don't think above is good solution. Since i faced the same issue where jersey springboot jackson has to be provided.

here above JacksonFeature.class is from glassfish which has less feature which is problem for springboot application in future

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


import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

@Provider
public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {

private final ObjectMapper mapper;

public ObjectMapperContextResolver() {
mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

@Override
public ObjectMapper getContext(Class<?> type) {
return mapper;
}

}

Important point is you need to import ObjectMapper as com.fasterxml.jackson.databind.ObjectMapper; for latest springboot Jackson

like image 3
Nisarg Bhagavantanavar Avatar answered Nov 19 '22 06:11

Nisarg Bhagavantanavar