java - org.springframework.data.mapping.MappingException:无法将类型类 java.util.ArrayList 转换为类 java.lang.Object 的实例
问题描述
从数据库中检索数据时出现此错误(MongoDB 驱动程序 V3.10.2、springboot V2.0、spring cloud V-Finchley.M9)。
最近我们将springboot版本从1.3升级到2.0,mongodb驱动升级到最新一次。在升级此代码之前可以正常工作,但现在它突然停止工作。
Java 类 POJO:
public class Positions {
private String type ="Polygon";
private List<List<List<Double>>> coordinates;
public List<List<List<Double>>> getCoordinates() {
return coordinates;
}
public void setCoordinates(List<List<List<Double>>> coordinates) {
this.coordinates = coordinates;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
我正在尝试映射的 MongoDB 数据:
"positionsFrom" : {
"type" : "Polygon",
"coordinates" : [
[
[
13.9092758594177,
44.8984249954859
],
[
13.921764224591,
44.8985769926884
],
[
13.9218071399353,
44.901373669501
], [
13.9092758594177,
44.8984249954859
]
]
]
}
错误日志:
org.springframework.data.mapping.MappingException: Cannot convert [14.245518205126928, 50.112434905459665] of type class java.util.ArrayList into an instance of class java.lang.Object! Implement a custom Converter<class java.util.ArrayList, class java.lang.Object> and register it with the CustomConversions. Parent object was: ClassPojo [direction = null, originCity = null, destinationCity = null, effectiveStartDate = 2019-04-11, effectiveEndDate = 2019-12-28, destination = null, currency = com.darwin.domain.model.Currency@490a218f, routeNumber = null, pickUp = null, _id = 5cb56d7db04d6442ad1ec487, adultRate = null, childRate = null, vehicleType = null, capacity = null] -> com.darwin.domain.localTransfers.LocalTransfer$Positions@77c9fddd
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:980)
at org.springframework.data.mongodb.core.convert.MappingMongoConverter.readCollectionOrArray(MappingMongoConverter.java:985)
at
解决方案
使用 org.springframework.data.mongodb.core.convert Customconversions 而不是 Mongocustomcoversions。无需编写额外的转换,但请记住旧的 Customconversions 已弃用。这是快速修复。
推荐阅读
- angular - 组件选择器不是已知元素
- typescript - 为什么可选对象成员的类型为“从不”?
- android - 如何设置 LiveData 以执行基于多个参数的搜索?
- javascript - Web Components 和 Shadow DOM 如何防止跨组件泄漏 CSS?
- python - 带有argparse参数的相同python命令在shell脚本中不起作用
- stanford-nlp - 用 RegexNER 而不是 CRF 模型覆盖命名实体
- javascript - 如何更新状态中存在的 JSX 元素的状态?
- html - 在 iOS 中选择未对齐的选项
- vagrant - 通过代理连接到 vagrant box - windows 10
- c - 为什么我在声明指针的时候不能打印已经初始化的指针值?