尝试将类映射到playframwork上的文档时发生MarshallingException

时间:2014-10-15 12:57:15

标签: java playframework runtime-error unmarshalling jongo

我正在用Java编写Play 2.3.2应用程序,我正在使用Jongo作为MongoDB的驱动程序。 在对我的代码进行一些更改后,我的应用程序给了我运行时错误 该错误被赋予getUser方法的第四行。 这是我用过的代码:

public static User getUser(String email) {
        Jongo jongo = MongoFactory.getJongo();
        MongoCollection users = jongo.getCollection(USERS_COLLECTION);
        return users.findOne("{email: '"+email +"'}").as(User.class);
}

Module课程添加到Company后,上述行中的方法as无法取消find的结果。

public class User {
    //... some fields ...
    private Company company;
    //... constructor using all fieds ...
    //... getters and setters for all fields ...
}

public class Company {
    //... some fields ...
    private List<Module> modules;
    //... constructor using all fieds ...
    //... getters and setters for all fields ...

    public class Module {
        private String name;
        private Date createTime;
        private Date expireTime;
        private Map<String, Object> options;

        //... constructor using all fieds ...
        //... getters and setters for all fields ...
    }

}

这是运行时错误:

    play.api.Application$$anon$1: Execution exception[[MarshallingException: Unable to unmarshall result to class bdrim.models.User from content { "_id" : { "$oid" : "53beb050d10e5ba896d8d912"} , "email" : "alberto@zero12.it" , "name" : "Alberto" , "surname" : "Simioni" , "password" : "$2a$10$E4hi2giYm4LnYq5e3TrfheGhsjtydWtl3GR/87.tUIA1Xs8MtY9kS" , "company" : { "name" : "Zero12 s.r.l." , "vat" : "04631710284" , "telephoneNumber" : "0499414411" , "country" : "Italy" , "city" : "Carmignano" , "postalCode" : "35010" , "address" : "Via spessa 25/c" , "modules" : [ { "name" : "mailchimp" , "createTime" : { "$date" : "2014-10-15T12:19:12.416Z"} , "expireTime" : { "$date" : "2015-10-15T12:19:12.416Z"} , "options" : { "listid" : "6d7e5ad5b8" , "apikey" : "9fe7bcf4d2ffff92306cf1de28acc9ea-us9"}}]} , "role" : "Super Administrator" , "idShop" : "Shop2"}]]
        at play.api.Application$class.handleError(Application.scala:296) ~[play_2.11-2.3.2.jar:2.3.2]
        at play.api.DefaultApplication.handleError(Application.scala:402) [play_2.11-2.3.2.jar:2.3.2]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.2.jar:2.3.2]
        at play.core.server.netty.PlayDefaultUpstreamHandler$$anonfun$3$$anonfun$applyOrElse$4.apply(PlayDefaultUpstreamHandler.scala:320) [play_2.11-2.3.2.jar:2.3.2]
        at scala.Option.map(Option.scala:145) [scala-library-2.11.2.jar:na]
Caused by: org.jongo.marshall.MarshallingException: Unable to unmarshall result to class bdrim.models.User from content { "_id" : { "$oid" : "53beb050d10e5ba896d8d912"} , "email" : "alberto@zero12.it" , "name" : "Alberto" , "surname" : "Simioni" , "password" : "$2a$10$E4hi2giYm4LnYq5e3TrfheGhsjtydWtl3GR/87.tUIA1Xs8MtY9kS" , "company" : { "name" : "Zero12 s.r.l." , "vat" : "04631710284" , "telephoneNumber" : "0499414411" , "country" : "Italy" , "city" : "Carmignano" , "postalCode" : "35010" , "address" : "Via spessa 25/c" , "modules" : [ { "name" : "mailchimp" , "createTime" : { "$date" : "2014-10-15T12:19:12.416Z"} , "expireTime" : { "$date" : "2015-10-15T12:19:12.416Z"} , "options" : { "listid" : "6d7e5ad5b8" , "apikey" : "9fe7bcf4d2ffff92306cfxxxxxacc9ea-us9"}}]} , "role" : "Super Administrator" , "idShop" : "Shop2"}
    at org.jongo.marshall.jackson.JacksonEngine.unmarshall(JacksonEngine.java:45) ~[jongo-1.1.jar:na]
    at org.jongo.ResultHandlerFactory$UnmarshallingResultHandler.map(ResultHandlerFactory.java:43) ~[jongo-1.1.jar:na]
    at org.jongo.FindOne.map(FindOne.java:51) ~[jongo-1.1.jar:na]
    at org.jongo.FindOne.as(FindOne.java:46) ~[jongo-1.1.jar:na]
    at bdrim.models.UsersDao.getUser(UsersDao.java:151) ~[classes/:na]
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class bdrim.models.Company$Module]: can not instantiate from JSON object (need to add/enable type information?)
 at [Source: de.undercouch.bson4jackson.io.LittleEndianInputStream@12adfe45; pos: 361] (through reference chain: bdrim.models.User["company"]->bdrim.models.Company["modules"])
    at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:164) ~[jackson-databind-2.4.1.jar:2.4.1]
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1065) ~[jackson-databind-2.4.1.jar:2.4.1]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:268) ~[jackson-databind-2.4.1.jar:2.4.1]
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:124) ~[jackson-databind-2.4.1.jar:2.4.1]
    at com.fasterxml.jackson.databind.deser.std.CollectionDeserializer.deserialize(CollectionDeserializer.java:232) ~[jackson-databind-2.4.1.jar:2.4.1]

1 个答案:

答案 0 :(得分:1)

Jackson无法实例化模块,因为它不知道如何调用Module构造函数。 您可以在每个构造函数参数上添加默认的私有空构造函数或添加@JsonProperty注释:

@JsonCreator
public Module(@JsonProperty("name") String name, @JsonProperty("createTime") Date createTime, @JsonProperty("expireTime")Date expireTime, @JsonProperty("options") Map<String, Object> options) {

OR

private Module() {//hidden jackson constructor}