我在MongoDB上使用SpringBoot,我有一个文档,如下图所示。
然后我映射到一个Java类:
@Document(collection = "instrument")
public abstract class Instrument extends BaseMongoEntity<String> {
@NotEmpty
private String feedProviderId;
@NotEmpty
private String code;
@NotEmpty
private String name;
private Map<String, Object> instrumentData;
private Map<String, Object> commonInfo;
private List<InstrumentHistoricalData> historicalData;
private List<DelayedInstrumentData> delayedData;
private String market;
// Getters, setters, builders, etc.
}
当然,instrumentData字段包含很多数据,但是出于参数的考虑,我只是在显示的文档中写了这两个。 所以我的问题是我无法将NOW_PRICE转换为BigDecimal。我可以毫无问题地编写它,从BigDecimal到Decimal128,但是没有其他方法。
我已经配置了读写器,如下所示:
@Configuration
public class MongoConfig {
@Bean
public MongoCustomConversions mongoCustomConversions() {
return new MongoCustomConversions(Arrays.asList(
new BigDecimalDecimal128Converter(),
new Decimal128BigDecimalConverter()
));
}
@WritingConverter
private static class BigDecimalDecimal128Converter implements
Converter<BigDecimal, Decimal128> {
@Override
public Decimal128 convert(@NonNull BigDecimal source) {
return new Decimal128(source);
}
}
@ReadingConverter
private static class Decimal128BigDecimalConverter implements
Converter<Decimal128, BigDecimal> {
@Override
public BigDecimal convert(@NonNull Decimal128 source) {
return source.bigDecimalValue();
}
}
}
因此,在检查MappingMongoConverter.class时,我注意到了这一点:
protected Map<Object, Object> readMap(TypeInformation<?> type, Bson bson, ObjectPath path) {
Assert.notNull(bson, "Document must not be null!");
Assert.notNull(path, "Object path must not be null!");
Class<?> mapType = typeMapper.readType(bson, type).getType();
TypeInformation<?> keyType = type.getComponentType();
TypeInformation<?> valueType = type.getMapValueType();
Class<?> rawKeyType = keyType != null ? keyType.getType() : null;
Class<?> rawValueType = valueType != null ? valueType.getType() : null;
Map<String, Object> sourceMap = asMap(bson);
Map<Object, Object> map = CollectionFactory.createMap(mapType, rawKeyType, sourceMap.keySet().size());
if (!DBRef.class.equals(rawValueType) && isCollectionOfDbRefWhereBulkFetchIsPossible(sourceMap.values())) {
bulkReadAndConvertDBRefMapIntoTarget(valueType, rawValueType, sourceMap, map);
return map;
}
for (Entry<String, Object> entry : sourceMap.entrySet()) {
if (typeMapper.isTypeKey(entry.getKey())) {
continue;
}
Object key = potentiallyUnescapeMapKey(entry.getKey());
if (rawKeyType != null && !rawKeyType.isAssignableFrom(key.getClass())) {
key = conversionService.convert(key, rawKeyType);
}
Object value = entry.getValue();
TypeInformation<?> defaultedValueType = valueType != null ? valueType : ClassTypeInformation.OBJECT;
if (value instanceof Document) {
map.put(key, read(defaultedValueType, (Document) value, path));
} else if (value instanceof BasicDBObject) {
map.put(key, read(defaultedValueType, (BasicDBObject) value, path));
} else if (value instanceof DBRef) {
map.put(key, DBRef.class.equals(rawValueType) ? value
: readAndConvertDBRef((DBRef) value, defaultedValueType, ObjectPath.ROOT, rawValueType));
} else if (value instanceof List) {
map.put(key, readCollectionOrArray(valueType != null ? valueType : ClassTypeInformation.LIST,
(List<Object>) value, path));
} else {
map.put(key, getPotentiallyConvertedSimpleRead(value, rawValueType));
}
}
return map;
}
因此,仅询问值是Document,BasicDBObject,DBRef还是List的实例。否则,它假定该值已被映射,而没有,因为它是一个数字值,并且没有考虑该可能性。
我想念什么吗?是否有解决此问题的方法?谢谢!
答案 0 :(得分:0)
如果您深入到getPotentiallyConvertedSimpleRead(…)
,您会发现我们检查CustomConversionInstance
是否已注册转换。我只能假设您的配置有问题,但是我看不到任何可怕的问题。您有一个可以共享的示例项目吗?