Jersey 2.6 Jackson provider registering
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.
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