我正在尝试将我的elasticsearch java客户端api版本从1.4.2更新到2.0.0。我使用的是java 1.8。我使用这段代码:
XContentBuilder builder = XContentFactory.jsonBuilder()
它曾经在1.4.2中工作,但我在2.0.0中得到了这个例外:
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.elasticsearch.common.xcontent.json.JsonXContentGenerator
at org.elasticsearch.common.xcontent.json.JsonXContent.newXContentGenerator(JsonXContent.java:69) ~[elasticsearch-2.0.0.jar:2.0.0]
at org.elasticsearch.common.xcontent.json.JsonXContent.createGenerator(JsonXContent.java:74) ~[elasticsearch-2.0.0.jar:2.0.0]
at org.elasticsearch.common.xcontent.json.JsonXContent.createGenerator(JsonXContent.java:80) ~[elasticsearch-2.0.0.jar:2.0.0]
at org.elasticsearch.common.xcontent.XContentBuilder.<init>(XContentBuilder.java:112) ~[elasticsearch-2.0.0.jar:2.0.0]
at org.elasticsearch.common.xcontent.XContentBuilder.<init>(XContentBuilder.java:102) ~[elasticsearch-2.0.0.jar:2.0.0]
at org.elasticsearch.common.xcontent.XContentBuilder.builder(XContentBuilder.java:80) ~[elasticsearch-2.0.0.jar:2.0.0]
at org.elasticsearch.common.xcontent.json.JsonXContent.contentBuilder(JsonXContent.java:40) ~[elasticsearch-2.0.0.jar:2.0.0]
at org.elasticsearch.common.xcontent.XContentFactory.contentBuilder(XContentFactory.java:122) ~[elasticsearch-2.0.0.jar:2.0.0]
at org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder(XContentFactory.java:49) ~[elasticsearch-2.0.0.jar:2.0.0]
在pom.xml中:
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>2.0.0</version>
</dependency>
答案 0 :(得分:13)
原因是我的依赖项列表中没有com.fasterxml.jackson.core,所以这导致了
java.lang.ClassNotFoundException: com.fasterxml.jackson.core.util.DefaultIndenter
我把它添加到我的pom.xml中:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
问题解决了!