我正在编写一个 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,如此处所述
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)
我不确定我做错了什么。我认为这是杰克逊 JSON 库,我不确定是否使用正确的库,因为它们在文档中的链接不再存在。
我感谢任何帮助