javarestdependency-injectiondropwizarddropwizard-guice

Guice Dependency Injection in dropwizard not working - is it a pom.xml artifacts dependency problem?


I want to run a Dropwizard (a REST Application).

I have a pom.xml file that uses old version of dropwizard (1.0.3) and it works. And there is another pom.xml available bellow for the new version (2.0.10) but it does not run.

Here is the working pom.xml (1.0.3 version of DropWizard) which is available on githut :

https://github.com/userInterview/dropwizard-guice

Just copy past the pom.xml to reproduce the error or the working version.

Here are the two pom.xml files :

I have that application available on github if someone wants to bring som help here :)

https://github.com/userInterview/dropwizard-guice

When i run the application i have the following error on console :

loizani@DevBoxHouse:~/git/dropwizard-guice-sample$ java -jar target/dwguice-1.0-SNAPSHOT.jar server config-dev.yml
WARN  [2021-02-17 23:39:04,518] com.squarespace.jersey2.guice.JerseyGuiceUtils: It appears jersey2-guice-spi is either not present or in conflict with some other Jar: ServiceLocatorGeneratorImpl(hk2-locator, 229995302)
INFO  [2021-02-17 22:39:05,636] io.dropwizard.server.DefaultServerFactory: Registering jersey handler with root path prefix: /
INFO  [2021-02-17 22:39:05,645] io.dropwizard.server.DefaultServerFactory: Registering admin handler with root path prefix: /
INFO  [2021-02-17 22:39:05,649] io.dropwizard.server.ServerFactory: Starting DWGuice
================================================================================

                              DWGuice

================================================================================

INFO  [2021-02-17 22:39:05,882] org.eclipse.jetty.setuid.SetUIDListener: Opened application@2516fc68{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
INFO  [2021-02-17 22:39:05,899] org.eclipse.jetty.setuid.SetUIDListener: Opened admin@304a9d7b{HTTP/1.1, (http/1.1)}{0.0.0.0:8081}
INFO  [2021-02-17 22:39:05,901] org.eclipse.jetty.server.Server: jetty-9.4.29.v20200521; built: 2020-05-21T17:20:40.598Z; git: 77c232aed8a45c818fd27232278d9f95a021095e; jvm 1.8.0_201-b09
java.lang.IncompatibleClassChangeError: Class org.glassfish.jersey.message.internal.MessagingBinders$HeaderDelegateProviders does not implement the requested interface org.glassfish.hk2.utilities.Binder
        at com.squarespace.jersey2.guice.JerseyGuiceUtils.bind(JerseyGuiceUtils.java:337)
        at com.squarespace.jersey2.guice.JerseyGuiceUtils.link(JerseyGuiceUtils.java:321)
        at com.squarespace.jersey2.guice.JerseyGuiceUtils.link(JerseyGuiceUtils.java:287)
        at com.squarespace.jersey2.guice.JerseyGuiceModule$ServiceLocatorProvider.get(JerseyGuiceModule.java:111)
        at com.squarespace.jersey2.guice.JerseyGuiceModule$ServiceLocatorProvider.get(JerseyGuiceModule.java:96)
        at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:85)
        at com.google.inject.internal.InternalFactoryToInitializableAdapter.provision(InternalFactoryToInitializableAdapter.java:57)
        at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:59)
        at com.google.inject.internal.InternalFactoryToInitializableAdapter.get(InternalFactoryToInitializableAdapter.java:47)
        at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
        at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:168)
        at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:39)
        at com.google.inject.internal.InternalInjectorCreator.loadEagerSingletons(InternalInjectorCreator.java:213)
        at com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:184)
        at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:111)
        at com.google.inject.internal.InjectorImpl.createChildInjector(InjectorImpl.java:246)
        at com.google.inject.internal.InjectorImpl.createChildInjector(InjectorImpl.java:251)
        at com.hubspot.dropwizard.guice.GuiceBundle$1.create(GuiceBundle.java:121)
        at com.squarespace.jersey2.guice.GuiceServiceLocatorGenerator.create(GuiceServiceLocatorGenerator.java:70)
        at org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.internalCreate(ServiceLocatorFactoryImpl.java:289)
        at org.glassfish.hk2.internal.ServiceLocatorFactoryImpl.create(ServiceLocatorFactoryImpl.java:245)
        at com.squarespace.jersey2.guice.GuiceServiceLocatorFactory.create(GuiceServiceLocatorFactory.java:88)
        at org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.createLocator(AbstractHk2InjectionManager.java:90)
        at org.glassfish.jersey.inject.hk2.AbstractHk2InjectionManager.<init>(AbstractHk2InjectionManager.java:62)
        at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.<init>(ImmediateHk2InjectionManager.java:38)
        at org.glassfish.jersey.inject.hk2.Hk2InjectionManagerFactory$Hk2InjectionManagerStrategy$1.createInjectionManager(Hk2InjectionManagerFactory.java:55)
        at org.glassfish.jersey.inject.hk2.Hk2InjectionManagerFactory.create(Hk2InjectionManagerFactory.java:73)
        at org.glassfish.jersey.internal.inject.Injections.createInjectionManager(Injections.java:69)
        at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:259)
        at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
        at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154)
        at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:347)
        at javax.servlet.GenericServlet.init(GenericServlet.java:180)
        at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:602)
        at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:413)
        at org.eclipse.jetty.servlet.ServletHandler.lambda$initialize$0(ServletHandler.java:749)
        at java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:352)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
        at java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
        at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:743)
        at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
        at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:773)
        at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:360)
        at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:844)
        at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:275)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
        at com.codahale.metrics.jetty9.InstrumentedHandler.doStart(InstrumentedHandler.java:110)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:117)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
        at org.eclipse.jetty.server.handler.gzip.GzipHandler.doStart(GzipHandler.java:425)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
        at org.eclipse.jetty.server.handler.StatisticsHandler.doStart(StatisticsHandler.java:255)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:169)
        at org.eclipse.jetty.server.Server.start(Server.java:408)
        at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
        at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:97)
        at org.eclipse.jetty.server.Server.doStart(Server.java:372)
        at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:72)
        at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
        at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:45)
        at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:87)
        at io.dropwizard.cli.Cli.run(Cli.java:78)
        at io.dropwizard.Application.run(Application.java:94)
        at ca.rasul.DWGuiceApplication.main(DWGuiceApplication.java:15)

Solution

  • Finally, the server starts normally if i use another kind of artifact, which is a modern one : "dropwizard-guicey" in version 5.2.0

    Remark : the enpoint "/brands/name" does not work ! It is because of an Injection problem. (beware !)

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <mainclass>com.dropwizard.application.MainApplication</mainclass>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>ru.vyarus</groupId>
            <artifactId>dropwizard-guicey</artifactId>
            <version>5.2.0</version>
        </dependency>
    </dependencies>