繁体   English   中英

org.springframework.data.mapping.MappingException:无法将类型类 java.util.ArrayList 转换为类 java.lang.Object 的实例

[英]org.springframework.data.mapping.MappingException: Cannot convert type class java.util.ArrayList into an instance of class 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 已弃用。 这是快速修复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM