我正在尝试将我的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>
原因是我的依赖项列表中没有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>
问题解决了!