0

I'm trying to figure how to explain Jersey and Jackson how to deserialize a Future that I pass as byte[].

I create my own ContextResolver

public class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {

    private static ObjectMapper mapper = null;

    public ObjectMapperContextResolver() {
        mapper = ObjectMapperFactory.getObjectMapper();     
    }

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

}

And the implementation of the ObjectMapper

public static ObjectMapper getObjectMapper() {
        ObjectMapper defaultObjectMapper = new ObjectMapper();
        SimpleModule futureModule = new SimpleModule("FutureModule");
        futureModule.<Future>addDeserializer(Future.class, new FutureDeserializer<String>());
        defaultObjectMapper.registerModule(futureModule);
        return defaultObjectMapper;
}

And then finally in the implementation of my FutureDeserializer

public class FutureDeserializer<T> extends StdDeserializer<Future<T>>{

    public FutureDeserializer() {
        super(Future.class);
    }

    @Override
    public Future<T> deserialize(JsonParser jp, DeserializationContext ctxt)
            throws IOException {
        ObjectMapper mapper=(ObjectMapper)jp.getCodec();
        //TODO: Breakpoint never stop here
        return null;
    }
}

Then I register in my ResourceConfig before start the JerseyTest

ResourceConfig rc = new ResourceConfig();
        rc.register(SpringLifecycleListener.class);
        rc.register(RequestContextFilter.class);
        rc.register(new JacksonFeature());
        rc.register(new ObjectMapperContextResolver());

But when I run the test the ObjectMapperContextResolver is invoked and the mapper returned to Jersey, but he never use the FutureDeserializer.

Any idea what I´m doing wrong?

paul
  • 12,873
  • 23
  • 91
  • 153
  • Your code doesn't show where you are using the ObjectMapper to de-serialize. Also, how you de-serialize is not clear, since the method is just returning null. – A_C Oct 25 '18 at 13:44
  • Also, take a look at if it helps: https://stackoverflow.com/questions/16757724/how-to-hook-jackson-objectmapper-with-guice-jersey?rq=1 – A_C Oct 25 '18 at 13:47

0 Answers0