arangodb自定义JsonDeserializer用于文档

时间:2016-05-23 14:02:35

标签: java jackson arangodb json-deserialization

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

MyDocument是我存储在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$12.construct(ConstructorConstructor.java:210)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:186)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.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$1.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)

1 个答案:

答案 0 :(得分:1)

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