How to hook Jackson ObjectMapper with Guice / Jersey

前端 未结 1 749
渐次进展
渐次进展 2021-02-01 09:37

I can\'t seem to get my Jackson ObjectMapper Module registered correctly.

I\'m using a Guice + Jersey + Jackson (FasterXML) stack.

I\'ve followed how to customis

1条回答
  •  伪装坚强ぢ
    2021-02-01 10:17

    I'm stuck in a typical guice--annotations-mystery, where it's practically untraceable to what I'm actually supposed to be doing. Spring users just report registering the component, and the container just picks it up.

    You really should read excellent Guice documentation. Guice is very easy to use, it has very small number of basic concepts. Your problem is in that you mixed Jersey JAX-RS dependency injection and Guice dependency injection. If you are using GuiceContainer then you declare that you will be using Guice for all of your DI, so you have to add bindings with Guice and not with JAX-RS.

    For instance, you do not need ContextResolver, you should use plain Guice Provider instead:

    import com.google.inject.Provider;
    
    public class ObjectMapperProvider implements Provider {
        @Override
        public ObjectMapper get() {
            final ObjectMapper mapper = new ObjectMapper();
            mapper.registerModule(new Hibernate4Module());
            return mapper;
        }
    }
    

    Then you should add corresponding binding to your module:

    bind(ObjectMapper.class).toProvider(ObjectMapperProvider.class).in(Singleton.class);
    

    This will bind ObjectMapper, but it is not enough to use Jersey with Jackson. You will need some kind of MessageBodyReader/MessageBodyWriter, e.g. JacksonJsonProvider. You will need another provider for it:

    public class JacksonJsonProviderProvider implements Provider {
        private final ObjectMapper mapper;
    
        @Inject
        JacksonJsonProviderProvider(ObjectMapper mapper) {
            this.mapper = mapper;
        }
    
        @Override
        public JacksonJsonProvider get() {
            return new JacksonJsonProvider(mapper);
        }
    }
    

    Then bind it:

    bind(JacksonJsonProvider.class).toProvider(JacksonJsonProviderProvider.class).in(Singleton.class);
    

    This is all you need to do - no subclassing is needed.

    There is a room for code size optimization though. If I were you I would use @Provides-methods:

    @Provides @Singleton
    ObjectMapper objectMapper() {
        final ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new Hibernate4Module());
        return mapper;
    }
    
    @Provides @Singleton
    JacksonJsonProvider jacksonJsonProvider(ObjectMapper mapper) {
        return new JacksonJsonProvider(mapper);
    }
    

    These methods should be added to one of your modules, e.g. to anonymous ServletModule. Then you won't need separate provider classes.
    BTW, you should use JerseyServletModule instead of plain ServletModule, it provides a lot of useful bindings for you.

    0 讨论(0)
提交回复
热议问题