0

我正在尝试使用 spring-data-mongo 从 mongo 集合中查询整数列表,但是在生成结果时会引发异常,因为 Integer 没有默认构造函数:

Query query = Query.query(Criteria.where("acceptsEmails").is(true));
query.fields().include("userId");
return mongoTemplate.find(query, Integer.class);

问题是生成了以下异常,因为它似乎尝试使用默认构造函数(显然不存在)实例化 Integer。

    org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [java.lang.Integer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Integer.<init>()
        at org.springframework.data.mapping.model.BeanWrapper.<init>(BeanWrapper.java:105) ~[spring-data-commons-core-1.2.0.RELEASE.jar:na]
        at org.springframework.data.mapping.model.BeanWrapper.create(BeanWrapper.java:73) ~[spring-data-commons-core-1.2.0.RELEASE.jar:na]
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:239) ~[spring-data-mongodb-1.0.0.RELEASE.jar:na]
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:187) ~[spring-data-mongodb-1.0.0.RELEASE.jar:na]
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:151) ~[spring-data-mongodb-1.0.0.RELEASE.jar:na]
        at org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:73) ~[spring-data-mongodb-1.0.0.RELEASE.jar:na]
        at org.springframework.data.mongodb.core.MongoTemplate$ReadDbObjectCallback.doWith(MongoTemplate.java:1693) ~[spring-data-mongodb-1.0.0.RELEASE.jar:na]
        at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:1444) ~[spring-data-mongodb-1.0.0.RELEASE.jar:na]
        at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1259) ~[spring-data-mongodb-1.0.0.RELEASE.jar:na]
        at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1248) ~[spring-data-mongodb-1.0.0.RELEASE.jar:na]
        at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:471) ~[spring-data-mongodb-1.0.0.RELEASE.jar:na]
        at ...
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.lang.Integer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Integer.<init>()
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:107) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        at org.springframework.data.mapping.model.BeanWrapper.<init>(BeanWrapper.java:102) ~[spring-data-commons-core-1.2.0.RELEASE.jar:na]
        ... 74 common frames omitted
Caused by: java.lang.NoSuchMethodException: java.lang.Integer.<init>()
        at java.lang.Class.getConstructor0(Class.java:2706) ~[na:1.6.0_24]
        at java.lang.Class.getDeclaredConstructor(Class.java:1985) ~[na:1.6.0_24]
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104) ~[spring-beans-3.1.1.RELEASE.jar:3.1.1.RELEASE]
        ... 75 common frames omitted

关于获取返回整数列表的解决方法的任何想法?这是一个非常大的整数列表,所以我不想使用中间格式,例如字符串,然后我必须对其进行迭代并转换为整数。

4

1 回答 1

1

可能您可以将自定义转换器从DbObject(您实际从驱动程序获得的数据类型)注册到Integer.

可能这样的事情会起作用:

public class UserIdReadConverter implements Converter<DBObject, Integer> {
    public Integer convert(DBObject source) {
        return Integer.parseInt((String) source.get("userId"));
    }
}

不幸的是,您仍然需要将结果值转换为整数。如果此解决方案可能是有用的解决方法,您可以在文档中查看。

http://static.springsource.org/spring-data/data-mongodb/docs/1.0.3.RELEASE/reference/html/#mongo.custom-converters

再见卡洛

于 2012-07-24T16:50:18.000 回答