arangodb 为文档自定义 JsonDeserializer

arangodb custom JsonDeserializer for document

我正在尝试为从 arangodb 获得的对象设置自定义反序列化器。

MyDocument 是 class 我存储在 arangodb:

public class MyDocument {

     private MyContent content;
     ...

     public MyContent getContent() {
       return content;
     }

     public setContent(MyContent content) {
       this.content = content;
     }
}

MyContent 是我想要自定义反序列化器的接口:

@JsonDeserialize(using = MyContentDeserializer.class)
public interface MyContent {
    ...
}

MyContentDeserializer 看起来像这样:

 public class MyContentDeserializer extends JsonDeserializer<MyContent> {
 @Override
     public DocumentContent deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        ...
     }
 }

当我尝试使用

从数据库中获取 MyDocument 时
 arangoDriver.getDocument("myDocument", key, MyDocument.class)

MyContentDeserializer.deserialize 方法没有执行,而是出现异常:

java.lang.RuntimeException: Unable to invoke no-args constructor for interface MyContent. Register an InstanceCreator with Gson for this type may fix this problem.
at com.google.gson.internal.ConstructorConstructor.construct(ConstructorConstructor.java:210)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:186)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.read(ReflectiveTypeAdapterFactory.java:103)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:196)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:875)
at com.google.gson.Gson.deserialize(Gson.java:128)
at com.arangodb.entity.EntityDeserializers$DocumentEntityDeserializer.deserialize(EntityDeserializers.java:753)
at com.arangodb.entity.EntityDeserializers$DocumentEntityDeserializer.deserialize(EntityDeserializers.java:728)
at com.google.gson.TreeTypeAdapter.read(TreeTypeAdapter.java:58)
at com.google.gson.Gson.fromJson(Gson.java:810)
at com.google.gson.Gson.fromJson(Gson.java:775)
at com.google.gson.Gson.fromJson(Gson.java:724)
at com.arangodb.entity.EntityFactory.createEntity(EntityFactory.java:162)
at com.arangodb.BaseArangoDriver.createEntityImpl(BaseArangoDriver.java:367)
at com.arangodb.BaseArangoDriver.createEntityWithFallback(BaseArangoDriver.java:259)
at com.arangodb.BaseArangoDriver.createEntity(BaseArangoDriver.java:236)
at com.arangodb.BaseArangoDriver.createEntity(BaseArangoDriver.java:311)
at com.arangodb.impl.InternalDocumentDriverImpl.getDocument(InternalDocumentDriverImpl.java:262)
at com.arangodb.ArangoDriver.getDocument(ArangoDriver.java:1908)
at com.arangodb.ArangoDriver.getDocument(ArangoDriver.java:1893)

@JsonDeserializeJackson 注释,您的堆栈跟踪告诉我们您使用 Gson 进行反序列化。所以你必须注册 Gson 反序列化器或使用 Jackson