簡體   English   中英

jongo / jackson反序列化具有通用屬性的類

[英]jongo/jackson deserialize a class with generic property

在具有復雜groupB的Java mongo項目中,
我嘗試反序列化這樣的json

{
  "_id" : {
    "TAB_COD" : "DOC1",
    "DON_COD" : "TXEX42030164"
  },
  "count" : 1.0
}

到這樣的通用java類:

public class AggregateResult<T extends MongoPojo> {

    public AggregateResult() {
    }

    @JsonProperty("_id")
    T id;
    @JsonProperty("count")
    long count;
}

為此,我使用DAO定義如下:

public class MyDAO extends DAO<MyDataClass> {

}

public class MyDataClass extends MongoPojo {
  @JsonProperty("TAB_COD")
  String tabCod;

  @JsonProperty("DON_COD")
  String donCod;
}

public abstract class DAO<T extends MongoPojo> {

public void group(...){
collection().aggregate(...).and(...)
            .and(...).as(AggregateResult.class));
}

當我調用我的Dao group()方法時,我有這個stacktrace

org.jongo.marshall.MarshallingException: Unable to unmarshall result to class com......AggregateResult from content { "_id" : { "TAB_COD" : "DOC1" , "DON_COD" : "TXEX42030164"} , "count" : 1}
    at org.jongo.marshall.jackson.JacksonEngine.unmarshall(JacksonEngine.java:50)
    at org.jongo.ResultHandlerFactory$UnmarshallingResultHandler.map(ResultHandlerFactory.java:43)
    at org.jongo.Aggregate$ResultsIterator.next(Aggregate.java:98)
    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 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com......MongoPojo, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
 at [Source: de.undercouch.bson4jackson.io.LittleEndianInputStream@28f3b248; pos: 5] (through reference chain: com......AggregateResult["_id"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:216)
    at com.fasterxml.jackson.databind.DeserializationContext.instantiationException(DeserializationContext.java:908)
    at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserialize(AbstractDeserializer.java:150)
    at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:490)
    at com.fasterxml.jackson.databind.deser.impl.FieldProperty.deserializeAndSet(FieldProperty.java:101)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:260)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:125)
    at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:1575)
    at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1234)
    at org.jongo.marshall.jackson.JacksonEngine.unmarshall(JacksonEngine.java:47)
    ... 28 more

您能幫我找到一種反序列化具有通用屬性的類的解決方案嗎? 謝謝你的幫助

您的POJO類應該implements Serializable並且您的@JsonIgnoreProperties(ignoreUnknown=true)實現AggregateResult類

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM