我已经创建了自定义转换器,将arrayList转换为对象,然后将此自定义转换器设置为MongoConverter,然后创建了MongoTemplate,但是当从mongo中获取数据并尝试转换为自定义类时,该自定义转换器无法正常工作
这是我的代码:
public class MongoListToObjectConverter implements Converter<ArrayList, Object>{
@Override
public Object convert(ArrayList source) {
return (Object) source;
}
}
@Component
public class MongoConfig {
@Autowired
MongoDbFactory mongoDbFactory;
@Bean
public MongoTemplate mongoTemplate() throws Exception {
MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory),
new MongoMappingContext());
converter.setCustomConversions(new CustomConversions(Arrays.asList(new MongoListToObjectConverter())));
converter.afterPropertiesSet();
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, converter);
return mongoTemplate;
}
}
以下是我从mongo中获取数据的方法。
@Override
public CloseableIterator<Map> findbyIorIdCreatedAtBetween(long ageGTE, long ageLT, String iorId, String collection) {
return mongoTemplate.stream(new Query().addCriteria(new Criteria().andOperator(Criteria.where("iorId").is(iorId),
Criteria.where("l_orderCreatedAt").gte(ageGTE).lte(ageLT))), Map.class, collection);
}
即使在编写此代码后,arraylist仍未转换为对象,并且我收到错误消息,说“实施自定义转换器并向CustomConversions注册它”。