Dropwizard: java.lang.IllegalStateException: 無法取得記錄器上下文

2022-09-04 21:52:37

我有一个项目,其中有多个模块。其中一个模块,比如说“main”,就是让我的服务类“MyService.class”。Dropwizard有一个jar - dropwizard-core。我希望这个jar在多个模块中,所以我正在考虑将其依赖性保留在父POM中。通过这样做,在运行我的dropwizard服务文件时,我遇到了如下异常:

SLF4J: Class path contains multiple SLF4J bindings.<br>
SLF4J: Found binding in [jar:file:/home/username/.m2/repository/org/slf4j/slf4j-simple/1.7.12/slf4j-simple-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]<br>
SLF4J: Found binding in [jar:file:/home/username/.m2/repository/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]<br>
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.<br>
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]<br><br>
Exception in thread "main" java.lang.IllegalStateException: Unable to acquire the logger context
                at io.dropwizard.logging.LoggingUtil.getLoggerContext(LoggingUtil.java:46)
                at io.dropwizard.logging.BootstrapLogging.bootstrap(BootstrapLogging.java:45)
                at io.dropwizard.logging.BootstrapLogging.bootstrap(BootstrapLogging.java:34)
                at io.dropwizard.Application.<init>(Application.java:24)
                at my.project.package.MyService.<init>(MyService.java:31)
                at my.project.package.MyService.main(MyService.java:38)

如果我将依赖项放在“主”模块的POM中,则该项目运行良好。

Dropwizard与错误的记录器绑定,这就是此异常背后的原因。但是这种绑定发生在后台,我无法控制它。

我还共享了这两种方案的依赖关系树。

案例 1 : 当 dropwizard-core 位于子 POM(主模块)中时:

my.groupId.myProject:myProject-main:jar:1.0-SNAPSHOT<br>
[INFO] +- io.dropwizard:dropwizard-core:jar:0.9.2:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-util:jar:0.9.2:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.0:compile<br>
[INFO] |  |  +- com.google.guava:guava:jar:18.0:compile<br>
[INFO] |  |  +- com.google.code.findbugs:jsr305:jar:3.0.1:compile<br>
[INFO] |  |  \- joda-time:joda-time:jar:2.9:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-jackson:jar:0.9.2:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-core:jar:2.6.3:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.3:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk7:jar:2.6.3:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-guava:jar:2.6.3:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.module:jackson-module-afterburner:jar:2.6.3:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-joda:jar:2.6.3:compile<br>
[INFO] |  |  +- org.slf4j:slf4j-api:jar:1.7.12:compile<br>
[INFO] |  |  \- ch.qos.logback:logback-classic:jar:1.1.3:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-validation:jar:0.9.2:compile<br>
[INFO] |  |  +- org.hibernate:hibernate-validator:jar:5.2.2.Final:compile<br>
[INFO] |  |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile<br>
[INFO] |  |  |  +- org.jboss.logging:jboss-logging:jar:3.2.1.Final:compile<br>
[INFO] |  |  |  \- com.fasterxml:classmate:jar:1.1.0:compile<br>
[INFO] |  |  \- org.glassfish:javax.el:jar:3.0.0:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-configuration:jar:0.9.2:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.6.3:compile<br>
[INFO] |  |  |  \- org.yaml:snakeyaml:jar:1.15:compile<br>
[INFO] |  |  \- org.apache.commons:commons-lang3:jar:3.4:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-logging:jar:0.9.2:compile<br>
[INFO] |  |  +- io.dropwizard.metrics:metrics-logback:jar:3.1.2:compile<br>
[INFO] |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.12:compile<br><br>
[INFO] |  |  +- ch.qos.logback:logback-core:jar:1.1.3:compile<br>
[INFO] |  |  +- org.slf4j:log4j-over-slf4j:jar:1.7.12:compile<br>
[INFO] |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile<br>
[INFO] |  |  \- org.eclipse.jetty:jetty-util:jar:9.2.13.v20150730:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-metrics:jar:0.9.2:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-jersey:jar:0.9.2:compile<br>
[INFO] |  |  +- org.glassfish.jersey.core:jersey-server:jar:2.22.1:compile<br>
[INFO] |  |  |  +- org.glassfish.jersey.core:jersey-common:jar:2.22.1:compile<br>
[INFO] |  |  |  |  +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.22.1:compile<br>
[INFO] |  |  |  |  \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile<br>
[INFO] |  |  |  +- javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile<br>
[INFO] |  |  |  +- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.22.1:compile<br>
[INFO] |  |  |  +- javax.annotation:javax.annotation-api:jar:1.2:compile<br>
[INFO] |  |  |  +- org.glassfish.hk2:hk2-api:jar:2.4.0-b31:compile<br>
[INFO] |  |  |  |  +- org.glassfish.hk2:hk2-utils:jar:2.4.0-b31:compile<br>
[INFO] |  |  |  |  \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.4.0-b31:compile<br>
[INFO] |  |  |  +- org.glassfish.hk2.external:javax.inject:jar:2.4.0-b31:compile<br>
[INFO] |  |  |  \- org.glassfish.hk2:hk2-locator:jar:2.4.0-b31:compile<br>
[INFO] |  |  |     \- org.javassist:javassist:jar:3.18.1-GA:compile<br>
[INFO] |  |  +- org.glassfish.jersey.ext:jersey-metainf-services:jar:2.22.1:compile<br>
[INFO] |  |  +- org.glassfish.jersey.ext:jersey-bean-validation:jar:2.22.1:compile<br>
[INFO] |  |  +- io.dropwizard.metrics:metrics-jersey2:jar:3.1.2:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.6.3:compile<br>
[INFO] |  |  |  +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.6.3:compile<br>
[INFO] |  |  |  \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.6.3:compile<br>
[INFO] |  |  +- org.glassfish.jersey.containers:jersey-container-servlet:jar:2.22.1:compile<br>
[INFO] |  |  |  \- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.22.1:compile<br>
[INFO] |  |  +- org.eclipse.jetty:jetty-server:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  |  +- javax.servlet:javax.servlet-api:jar:3.1.0:compile<br>
[INFO] |  |  |  \- org.eclipse.jetty:jetty-io:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  +- org.eclipse.jetty:jetty-webapp:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  |  \- org.eclipse.jetty:jetty-xml:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  \- org.eclipse.jetty:jetty-continuation:jar:9.2.13.v20150730:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-servlets:jar:0.9.2:compile<br>
[INFO] |  |  \- io.dropwizard.metrics:metrics-annotation:jar:3.1.2:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-jetty:jar:0.9.2:compile<br>
[INFO] |  |  +- io.dropwizard.metrics:metrics-jetty9:jar:3.1.2:compile<br>
[INFO] |  |  +- org.eclipse.jetty:jetty-servlet:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  |  \- org.eclipse.jetty:jetty-security:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  +- org.eclipse.jetty:jetty-servlets:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  \- org.eclipse.jetty:jetty-http:jar:9.2.13.v20150730:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-lifecycle:jar:0.9.2:compile<br>
[INFO] |  +- io.dropwizard.metrics:metrics-core:jar:3.1.2:compile<br>
[INFO] |  +- io.dropwizard.metrics:metrics-jvm:jar:3.1.2:compile<br>
[INFO] |  +- io.dropwizard.metrics:metrics-servlets:jar:3.1.2:compile<br>
[INFO] |  |  \- io.dropwizard.metrics:metrics-json:jar:3.1.2:compile<br>
[INFO] |  +- io.dropwizard.metrics:metrics-healthchecks:jar:3.1.2:compile<br>
[INFO] |  +- net.sourceforge.argparse4j:argparse4j:jar:0.6.0:compile<br>
[INFO] |  \- org.eclipse.jetty.toolchain.setuid:jetty-setuid-java:jar:1.0.3:compile<br>
[INFO] +- de.thomaskrille:dropwizard-template-config:jar:1.1.0:compile<br>
[INFO] +- my.groupId.myProject:myProject-module1:jar:1.0-SNAPSHOT:compile<br>
[INFO] +- my.groupId.myProject:myProject-module2:jar:1.0-SNAPSHOT:compile<br>
[INFO] +- my.groupId.myProject:myProject-module3:jar:0.0.1-SNAPSHOT:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-auth:jar:0.9.1:compile<br>
[INFO] |  \- io.dropwizard:dropwizard-client:jar:0.9.1:compile<br>
[INFO] |     +- org.glassfish.jersey.core:jersey-client:jar:2.22.1:compile<br>
[INFO] |     +- org.apache.httpcomponents:httpclient:jar:4.5.1:compile<br>
[INFO] |     |  +- org.apache.httpcomponents:httpcore:jar:4.4.3:compile<br>
[INFO] |     |  \- commons-codec:commons-codec:jar:1.9:compile<br>
[INFO] |     +- io.dropwizard.metrics:metrics-httpclient:jar:3.1.2:compile<br>
[INFO] |     \- org.glassfish.jersey.connectors:jersey-apache-connector:jar:2.22.1:compile<br>
[INFO] +- junit:junit:jar:4.12:test<br>
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test<br>
[INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test<br>
[INFO] \- org.mockito:mockito-core:jar:1.10.19:test<br>
[INFO]    \- org.objenesis:objenesis:jar:2.1:test<br>

**Case 2 : When dropwizard-core is in parent POM :**

 my.groupId.myProject:myProject-main:jar:jar:1.0-SNAPSHOT<br>
[INFO] +- de.thomaskrille:dropwizard-template-config:jar:1.1.0:compile<br>
[INFO] +- my.groupId.myProject:myProject-module1:jar:1.0-SNAPSHOT:compile<br>
[INFO] +- my.groupId.myProject:myProject-module2:jar:1.0-SNAPSHOT:compile<br>
[INFO] +- my.groupId.myProject:myProject-common:jar:0.0.1-SNAPSHOT:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-auth:jar:0.9.1:compile<br>
[INFO] |  \- io.dropwizard:dropwizard-client:jar:0.9.1:compile<br>
[INFO] |     +- org.glassfish.jersey.core:jersey-client:jar:2.22.1:compile<br>
[INFO] |     |  +- javax.ws.rs:javax.ws.rs-api:jar:2.0.1:compile<br>
[INFO] |     |  +- org.glassfish.jersey.core:jersey-common:jar:2.22.1:compile<br>
[INFO] |     |  |  +- org.glassfish.jersey.bundles.repackaged:jersey-guava:jar:2.22.1:compile<br>
[INFO] |     |  |  \- org.glassfish.hk2:osgi-resource-locator:jar:1.0.1:compile<br>
[INFO] |     |  +- org.glassfish.hk2:hk2-api:jar:2.4.0-b31:compile<br>
[INFO] |     |  |  +- org.glassfish.hk2:hk2-utils:jar:2.4.0-b31:compile<br>
[INFO] |     |  |  \- org.glassfish.hk2.external:aopalliance-repackaged:jar:2.4.0-b31:compile<br>
[INFO] |     |  +- org.glassfish.hk2.external:javax.inject:jar:2.4.0-b31:compile<br>
[INFO] |     |  \- org.glassfish.hk2:hk2-locator:jar:2.4.0-b31:compile<br>
[INFO] |     |     \- org.javassist:javassist:jar:3.18.1-GA:compile<br>
[INFO] |     +- org.apache.httpcomponents:httpclient:jar:4.5.1:compile<br>
[INFO] |     |  +- org.apache.httpcomponents:httpcore:jar:4.4.3:compile<br>
[INFO] |     |  \- commons-codec:commons-codec:jar:1.9:compile<br>
[INFO] |     +- io.dropwizard.metrics:metrics-httpclient:jar:3.1.2:compile<br>
[INFO] |     \- org.glassfish.jersey.connectors:jersey-apache-connector:jar:2.22.1:compile<br>
[INFO] +- io.dropwizard:dropwizard-core:jar:0.9.2:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-util:jar:0.9.2:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.6.0:compile<br>
[INFO] |  |  +- com.google.guava:guava:jar:18.0:compile<br>
[INFO] |  |  +- com.google.code.findbugs:jsr305:jar:3.0.1:compile<br>
[INFO] |  |  \- joda-time:joda-time:jar:2.9:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-jackson:jar:0.9.2:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-core:jar:2.6.3:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.core:jackson-databind:jar:2.6.3:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-jdk7:jar:2.6.3:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-guava:jar:2.6.3:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.module:jackson-module-afterburner:jar:2.6.3:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.datatype:jackson-datatype-joda:jar:2.6.3:compile<br>
[INFO] |  |  +- org.slf4j:slf4j-api:jar:1.7.12:compile<br>
[INFO] |  |  \- ch.qos.logback:logback-classic:jar:1.1.3:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-validation:jar:0.9.2:compile<br>
[INFO] |  |  +- org.hibernate:hibernate-validator:jar:5.2.2.Final:compile<br>
[INFO] |  |  |  +- javax.validation:validation-api:jar:1.1.0.Final:compile<br>
[INFO] |  |  |  +- org.jboss.logging:jboss-logging:jar:3.2.1.Final:compile<br>
[INFO] |  |  |  \- com.fasterxml:classmate:jar:1.1.0:compile<br>
[INFO] |  |  \- org.glassfish:javax.el:jar:3.0.0:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-configuration:jar:0.9.2:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.6.3:compile<br>
[INFO] |  |  |  \- org.yaml:snakeyaml:jar:1.15:compile<br>
[INFO] |  |  \- org.apache.commons:commons-lang3:jar:3.4:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-logging:jar:0.9.2:compile<br>
[INFO] |  |  +- io.dropwizard.metrics:metrics-logback:jar:3.1.2:compile<br>
[INFO] |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.12:compile<br>
[INFO] |  |  +- ch.qos.logback:logback-core:jar:1.1.3:compile<br>
[INFO] |  |  +- org.slf4j:log4j-over-slf4j:jar:1.7.12:compile<br>
[INFO] |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.12:compile<br>
[INFO] |  |  \- org.eclipse.jetty:jetty-util:jar:9.2.13.v20150730:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-metrics:jar:0.9.2:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-jersey:jar:0.9.2:compile<br>
[INFO] |  |  +- org.glassfish.jersey.core:jersey-server:jar:2.22.1:compile<br>
[INFO] |  |  |  +- org.glassfish.jersey.media:jersey-media-jaxb:jar:2.22.1:compile<br>
[INFO] |  |  |  \- javax.annotation:javax.annotation-api:jar:1.2:compile<br>
[INFO] |  |  +- org.glassfish.jersey.ext:jersey-metainf-services:jar:2.22.1:compile<br>
[INFO] |  |  +- org.glassfish.jersey.ext:jersey-bean-validation:jar:2.22.1:compile<br>
[INFO] |  |  +- io.dropwizard.metrics:metrics-jersey2:jar:3.1.2:compile<br>
[INFO] |  |  +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:jar:2.6.3:compile<br>
[INFO] |  |  |  +- com.fasterxml.jackson.jaxrs:jackson-jaxrs-base:jar:2.6.3:compile<br>
[INFO] |  |  |  \- com.fasterxml.jackson.module:jackson-module-jaxb-annotations:jar:2.6.3:compile<br>
[INFO] |  |  +- org.glassfish.jersey.containers:jersey-container-servlet:jar:2.22.1:compile<br>
[INFO] |  |  |  \- org.glassfish.jersey.containers:jersey-container-servlet-core:jar:2.22.1:compile<br>
[INFO] |  |  +- org.eclipse.jetty:jetty-server:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  |  +- javax.servlet:javax.servlet-api:jar:3.1.0:compile<br>
[INFO] |  |  |  \- org.eclipse.jetty:jetty-io:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  +- org.eclipse.jetty:jetty-webapp:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  |  \- org.eclipse.jetty:jetty-xml:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  \- org.eclipse.jetty:jetty-continuation:jar:9.2.13.v20150730:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-servlets:jar:0.9.2:compile<br>
[INFO] |  |  \- io.dropwizard.metrics:metrics-annotation:jar:3.1.2:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-jetty:jar:0.9.2:compile<br>
[INFO] |  |  +- io.dropwizard.metrics:metrics-jetty9:jar:3.1.2:compile<br>
[INFO] |  |  +- org.eclipse.jetty:jetty-servlet:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  |  \- org.eclipse.jetty:jetty-security:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  +- org.eclipse.jetty:jetty-servlets:jar:9.2.13.v20150730:compile<br>
[INFO] |  |  \- org.eclipse.jetty:jetty-http:jar:9.2.13.v20150730:compile<br>
[INFO] |  +- io.dropwizard:dropwizard-lifecycle:jar:0.9.2:compile<br>
[INFO] |  +- io.dropwizard.metrics:metrics-core:jar:3.1.2:compile<br>
[INFO] |  +- io.dropwizard.metrics:metrics-jvm:jar:3.1.2:compile<br>
[INFO] |  +- io.dropwizard.metrics:metrics-servlets:jar:3.1.2:compile<br>
[INFO] |  |  \- io.dropwizard.metrics:metrics-json:jar:3.1.2:compile<br>
[INFO] |  +- io.dropwizard.metrics:metrics-healthchecks:jar:3.1.2:compile<br>
[INFO] |  +- net.sourceforge.argparse4j:argparse4j:jar:0.6.0:compile<br>
[INFO] |  \- org.eclipse.jetty.toolchain.setuid:jetty-setuid-java:jar:1.0.3:compile<br>
[INFO] +- junit:junit:jar:4.12:test<br>
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test<br>
[INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test<br>
[INFO] \- org.mockito:mockito-core:jar:1.10.19:test<br>
[INFO]    \- org.objenesis:objenesis:jar:2.1:test<br>

如果有人能提出一个可能的推理,那将是非常有帮助的。

谢谢。


答案 1

你的问题是你的类路径中有两个不同的slf4j实现(又名绑定),而事实并非如此。

发生这种情况时,slf4j 将使用第一个(按类路径中的出现顺序)。然后,dropwizard 尝试检索实际的,但期望它是 logback 的 实例。LoggingUtilILoggerFactoryLoggerContext

在你的例子中,它是的一个实例,因为当您在父POM中声明依赖关系时,它首先出现在类路径中:实际上,依赖关系是由.SimpleLoggerFactoryslf4j-simpledropwizard-corelogback-classicdropwizard-core

解决方案非常简单:您必须删除 对 的依赖关系。slf4j-simple

这在slf4j手册的开头就进行了解释。


答案 2

在我通过在我的build.gradle中添加以下内容来全局排除jar之后,这个问题已经为我解决了slf4j-log4j12

configurations.all {
    exclude group:"org.slf4j", module: "slf4j-log4j12"
}

推荐