Kiran Mungekar
Kiran Mungekar

Reputation: 21

org.springframework.data.mapping.MappingException: Cannot convert type class java.util.ArrayList into an instance of class java.lang.Object

Getting this error on retrieving data from database (MongoDB driver V3.10.2, springboot V2.0, spring cloud V-Finchley.M9).

Recently we have upgraded the springboot version from 1.3 to 2.0 and mongodb driver to latest once. Before upgrading this code was working but now it suddenly stopped working.

Java Class 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 data, which I am trying to map:

"positionsFrom" : {
        "type" : "Polygon",
        "coordinates" : [ 
            [ 
                [ 
                    13.9092758594177, 
                    44.8984249954859
                ], 
                [ 
                    13.921764224591, 
                    44.8985769926884
                ], 
                [ 
                    13.9218071399353, 
                    44.901373669501
                ], [ 
                    13.9092758594177, 
                    44.8984249954859
                ]
            ]
     ]
}

The error log:

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 

Upvotes: 2

Views: 2804

Answers (1)

Khalid Shaikh
Khalid Shaikh

Reputation: 11

Use org.springframework.data.mongodb.core.convert Customconversions instead of Mongocustomcoversions. There is no need to write extra conversion but remember old Customconversions is deprecated. This is quick fix.

Upvotes: 1

Related Questions