我正在编写一个Java应用程序以在CouchDB上插入数据。为此,我正在使用Ektorp,并且得到了异常org.ektorp.InvalidDocumentException: Cannot resolve id accessor in class
这些是我的Maven依赖项:
<dependency>
<groupId>org.ektorp</groupId>
<artifactId>org.ektorp</artifactId>
<version>1.4.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.10</version>
</dependency>
POJO,如here所述
public class Item {
@JsonProperty("_index")
public String index;
@JsonProperty("_type")
public String type;
@JsonProperty("_id")
public String id;
@JsonProperty("_score")
public float score;
}
仓库:
public class ItemRepository extends CouchDbRepositorySupport<Item> {
public ItemRepository(CouchDbConnector db) {
super(Item.class, db);
}
}
以及我要如何添加:
Item x = new Item();
x.id = "1291";
x.index="test_index";
x.score = 1;
x.type = "_doc";
this.repo = new ItemRepository(this.db);
this.repo.add(x);
错误说明:
Exception in thread "main" org.ektorp.InvalidDocumentException: Cannot resolve id accessor in class org.example.Item
at org.ektorp.util.Documents$MethodAccessor.assertMethodFound(Documents.java:165)
at org.ektorp.util.Documents$MethodAccessor.<init>(Documents.java:136)
at org.ektorp.util.Documents.getAccessor(Documents.java:113)
at org.ektorp.util.Documents.getRevision(Documents.java:77)
at org.ektorp.util.Documents.isNew(Documents.java:85)
at org.ektorp.support.CouchDbRepositorySupport.add(CouchDbRepositorySupport.java:100)
at org.example.DataBase.addItem(DataBase.java:69)
at org.example.App.main(App.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
我不确定自己在做什么错。我认为是我不确定Jackson的JSON库是否在使用正确的库,因为它们在文档中的链接已不存在。
感谢您的帮助
最佳答案
我解决了问题。这只是放置正确的依赖项的问题。这是正确的ID:
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.9.13</version>
</dependency>