jongo / jackson反序列化具有泛型属性的类

时间:2017-05-05 16:04:39

标签: java mongodb jackson aggregation-framework jongo

在具有复杂groupBy请求的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()方法时 我有这个堆栈跟踪

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

你能帮我找到一个用泛型属性反序列化一个类的解决方案吗? 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

您的POJO课程应为implements Serializable,您的@JsonIgnoreProperties(ignoreUnknown=true)应为AggregateResult课程