我的应用程序将部署在tcServer和WebSphere 6.1上。这个应用程序使用ehCache,因此需要slf4j作为依赖项。
因此,我将slf4j-api.jar (1.6) jar添加到war文件包中。
应用程序在tcServer中正常工作,除了以下错误:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
然而,当我在WebSphere中部署时,我得到了一个java.lang.NoClassDefFoundError: org.slf4j.impl.StaticLoggerBinder。
同时伴有加载类“org.slf4j.impl.StaticMDCBinder”失败
我已经检查了两个应用服务器的类路径,没有其他slf4j jar。
有人知道这里会发生什么吗?
他们的官网给出了解决方案:
Failed to load class org.slf4j.impl.StaticLoggerBinder
This warning message is reported when the
org.slf4j.impl.StaticLoggerBinder class could not be loaded into
memory. This happens when no appropriate SLF4J binding could be found
on the class path. Placing one (and only one) of slf4j-nop.jar
slf4j-simple.jar, slf4j-log4j12.jar, slf4j-jdk14.jar or
logback-classic.jar on the class path should solve the problem. SINCE
1.6.0 As of SLF4J version 1.6, in the absence of a binding, SLF4J will default to a no-operation (NOP) logger implementation. If you are
responsible for packaging an application and do not care about
logging, then placing slf4j-nop.jar on the class path of your
application will get rid of this warning message. Note that embedded
components such as libraries or frameworks should not declare a
dependency on any SLF4J binding but only depend on slf4j-api. When a
library declares a compile-time dependency on a SLF4J binding, it
imposes that binding on the end-user, thus negating SLF4J's purpose.
解决方案:我已经添加到我的项目中使用intellij的maven研究,我选择了slf4j-jdk14.jar。
您的问题很可能是因为<scope>test</scope>(在某些情况下也<scope>提供</scope>),正如前面提到的@thangaraj。
文档中说:
此范围表示正常情况下不需要依赖项
应用程序的使用,并且仅可用于测试编译
以及执行阶段。测试依赖关系是不可传递的,并且只存在于测试和执行类路径中。
所以,如果你不需要测试依赖项,那么你可以使用代替(你将在mvnrepository中看到的):
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.24</version>
<scope>test</scope>
</dependency>
没有任何作用域(默认情况下,如果没有提供其他作用域,则为编译作用域):
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.25</version>
</dependency>
这相当于:
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-nop -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.25</version>
<scope>compile</scope>
</dependency>
我没有添加任何依赖项,我只是改变了我使用它们的方式。
预览代码
(如果您使用的是弹性搜索版本< 7.0,则取消注释此代码)
IndexRequest indexRequest = new IndexRequest(
"twitter",
"tweets",
id // this is to make our consumer idempotent
).source(record.value(), XContentType.JSON);
当前代码
IndexRequest indexRequest = new IndexRequest("tweets")
.source(record.value(), XContentType.JSON)
.id(id); // this is to make our consumer idempotent
我使用bulkrequest,并删除了这个错误。