没有找到能够将GeoCommand转换为DBObject的转换器

时间:2017-09-12 11:08:22

标签: java mongodb spring-data spring-data-mongodb

我无法在spring数据mongodb中查询GeoSpatial Query within,无论是编写自定义查询还是弹簧数据接口查询我都收到此错误,同样的查询在mongodb中工作正常客户端(robomongo):

以下是查询:

distanceRepository.findDistanceBySrcWithin(srcCircle);

以下是mongo配置:

@Override
public MongoClient mongo() throws Exception {
List<MongoCredential> mongoCredentials = new ArrayList<>();
mongoCredentials.add(MongoCredential.createCredential(userName, "admin", password.toCharArray()));
return new MongoClient(new ServerAddress(host, port), mongoCredentials);
}

@Override
@Bean
public MongoTemplate mongoTemplate() throws Exception {
MultiTenantMongoDbFactory mongoDbFactory = (MultiTenantMongoDbFactory) mongoDbFactory();

MongoTypeMapper typeMapper = new DefaultMongoTypeMapper(null);
MappingMongoConverter converter = new MappingMongoConverter(mongoDbFactory(), new MongoMappingContext());
converter.setTypeMapper(typeMapper);

MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter);
mongoDbFactory.setMongoTemplate(mongoTemplate);
return mongoTemplate;
}

@Override
@Bean
public MongoDbFactory mongoDbFactory() throws Exception {
return new MultiTenantMongoDbFactory(mongo(), dbName);
}

以下是堆栈跟踪:

2017-09-12 15:51:00 ERROR [JobDurationServiceImpl]:250 - Error in method getDistance Reason: No converter found capable of converting from type [org.springframework.data.mongodb.core.query.GeoCommand] to type [com.mongodb.DBObject]
org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.mongodb.core.query.GeoCommand] to type [com.mongodb.DBObject]
at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:313)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:195)
at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:176)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.convertToMongoType(MappingMongoConverter.java:979)
at org.springframework.data.mongodb.core.convert.QueryMapper.delegateConvertToMongoType(QueryMapper.java:393)
at org.springframework.data.mongodb.core.convert.QueryMapper.convertSimpleOrDBObject(QueryMapper.java:381)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedValue(QueryMapper.java:327)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedKeyword(QueryMapper.java:277)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObjectForField(QueryMapper.java:215)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:130)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1760)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1750)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:624)
at org.springframework.data.mongodb.repository.query.MongoQueryExecution$CollectionExecution.execute(MongoQueryExecution.java:70)
at org.springframework.data.mongodb.repository.query.MongoQueryExecution$ResultProcessingExecution.execute(MongoQueryExecution.java:345)
at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:91)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy76.findDistanceBySrcWithin(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

1 个答案:

答案 0 :(得分:3)

关键是添加以下行:

converter.afterPropertiesSet();

MappingMongoConverter converter = new MappingMongoConverter(mongoDbFactory(), 
new MongoMappingContext());
converter.setTypeMapper(typeMapper);

创建MongoTemplate