FasterXML / jackson-modules-base

Uber-project for foundational modules of Jackson that build directly on core components but nothing else; not including data format or datatype modules
Apache License 2.0
167 stars 77 forks source link

[guice] fail to inject MapBinder #43

Open taodongl opened 6 years ago

taodongl commented 6 years ago

MapBinder

final class Configure {
    @Inject
    private Map<String, WorkspaceFactory> factoryMap;
    ... ...
}

When I want to deserialize json: Configure configure = mapper.readValue(file, Configure.class);. I get the following errors:

Exception in thread "main" com.google.inject.ConfigurationException: Guice configuration errors:

1) No implementation for java.util.Map was bound.
  while locating java.util.Map

1 error
    at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1075)
    at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1081)
    at com.fasterxml.jackson.module.guice.GuiceInjectableValues.findInjectableValue(GuiceInjectableValues.java:20)
    at com.fasterxml.jackson.databind.DeserializationContext.findInjectableValue(DeserializationContext.java:382)
    at com.fasterxml.jackson.databind.deser.impl.ValueInjector.findValue(ValueInjector.java:45)
    at com.fasterxml.jackson.databind.deser.impl.ValueInjector.inject(ValueInjector.java:51)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.injectValues(BeanDeserializerBase.java:1513)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:354)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2890)
cowtowncoder commented 6 years ago

I don't know this module well (or Guice), but from exception it sounds like there is no binding on Guice for type Map?

taodongl commented 6 years ago

I can inject successfully outside Jackson Module.

    public static void main(String[] args) {
        Injector injector = Guice.createInjector(Stage.PRODUCTION, new BootstrapModule());
        Configure configure = new Configure();
        injector.injectMembers(configure);
        ... ...
   }

So my workaround is: put MapBinder Inject into POJO class, and inject the POJO class in Configure.class:

final public class Manager {
    @Inject
    private Map<String, WorkspaceFactory> factoryMap;
    ... ...
}
final class Configure {
    @Inject
    private Manager manager;
    ... ...
}
... ...
Configure configure = mapper.readValue(file, Configure.class);