我试图使用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
有关获取整数列表的变通方法的任何想法?这是一个非常大的整数列表,所以我不想使用像String这样的中间格式,然后我必须迭代并转换为整数。
答案 0 :(得分:1)
您可以注册自定义转换器DbObject
(您实际从驱动程序获取的数据类型)到Integer
。
可能这样的事情可以解决问题:
public class UserIdReadConverter implements Converter<DBObject, Integer> {
public Integer convert(DBObject source) {
return Integer.parseInt((String) source.get("userId"));
}
}
不幸的是,您仍然需要将结果值转换为Integer。您可以在文档中看到此解决方案是否有用。
再见 卡罗