dropwizard 中的 Guice 依赖注入不起作用 - 它是 pom.xml 工件依赖问题吗?

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

我想要 运行 Dropwizard(REST 应用程序)。

我有一个 pom.xml 文件,它使用旧版本的 dropwizard (1.0.3),它可以工作。 对于新版本 (2.0.10),还有另一个 pom.xml 可用,但它没有 运行。

这是可用的 pom.xml(DropWizard 的 1.0.3 版本),可在 githut 上找到:

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

只需复制过去 pom.xml 即可重现错误或工作版本。

这是两个 pom.xml 文件:

如果有人想在这里提供帮助,我可以在 github 上使用该应用程序:)

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

当我 运行 应用程序时,我在控制台上出现以下错误:

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.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.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.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[=13=](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)

最后,如果我使用另一种神器,即现代神器:5.2.0 版中的“dropwizard-guicey”,服务器将正常启动

注意:enpoint“/brands/name”不起作用!这是因为注入问题。 (注意!)

<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>