Skip to main content
  1. Articles/

Q: Using Jackson ObjectMapper with Jersey

This was originally posted as an answer to the question "Using Jackson ObjectMapper with Jersey" on

EDIT: Don’t use the old approach below as it produces bugs (at least with with android device, see EDIT2 for more details). As of my tests, Jersey v2.6 seems to fix the problem with the @Provide , which approach did not work. I was able to get it work with this simple provider:

public class JerseyMapperProvider implements ContextResolver<ObjectMapper> {
    private static ObjectMapper apiMapper = ObjectMapperManager.createMapperForApi();
    public ObjectMapper getContext(Class<?> type)
        return apiMapper;

So please don’t use my hack from below.



public class MyObjectMapperProvider implements ContextResolver<ObjectMapper>

was not working for me (Jersey 2.4 & Jackson 2.3) and maybe this is due to a in the jackson provider reported bug in the code where the ContextResolver should be registered in (2.3rc1):

protected ObjectMapper _locateMapperViaProvider(Class<?> type, MediaType mediaType)
    if (_providers != null) {
        ContextResolver<ObjectMapper> resolver = _providers.getContextResolver(ObjectMapper.class, mediaType);
        /\* Above should work as is, but due to this bug
         \*   []
         \* in Jersey, it doesn't. But this works until resolution of
         \* the issue:
        if (resolver == null) {
            resolver = _providers.getContextResolver(ObjectMapper.class, null);
        if (resolver != null) {
            return resolver.getContext(type);
    return null;

But at least I cannot access, so I don’t know what this bug is about.

But I found a workaround (a hack if you so will). Just extend JacksonJsonProvider with the proper annotation and return your ObjectMapper like this:

@Consumes(MediaType.APPLICATION_JSON) // NOTE: required to support "non-standard" JSON variants
public class JacksonHackProvider extends JacksonJsonProvider {
    protected ObjectMapper _locateMapperViaProvider(Class<?> type, MediaType mediaType) {
        return new MyCustomObjectMapper();

No need to do anything it will register itself (check with log, it will register the first time you access a json rest service). This is now working for me, not elegant, but I gave up.

EDIT: Use with caution - Im experiencing a bug maybe related to this hack: Android volley cannot send a POST/PUT request with a request body, always getting 400 from the framework, I will investigate and report my findings.

EDIT2: This hack was indeed responsible for a generic 400 whenever an Android app with volley and OKHTTP client tried tried to do a POST or PUT request so don’t use this - in my test jersey 2.6 seems to fix this so you can use @Provide approach