Reputation: 410
I'm trying to get all documents inside a collection and add them to a List
as java objects, however when I run findAll
, I get the following exception:
java.lang.ClassNotFoundException: org.springframework.data.convert.CollectionFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_65]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_65]
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_65]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_65]
...
And this is the related error log:
2016-01-17 20:54:05.956 ERROR 87437 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/data/convert/CollectionFactory] with root cause
Here's the code that results in the exception:
public List<TruckEntity> findAll() {
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoOperations mongo = new MongoTemplate(mongoClient, "trucksDb");
List<TruckEntity> trucks = mongo.findAll(TruckEntity.class);
return trucks;
}
the line List<TruckEntity> trucks = mongo.findAll(TruckEntity.class);
is where the exception is thrown.
When I use
FindIterable<Document> iterable = db.getCollection("restaurants").find();
I can access the documents using the iterator, so the documents are there as TruckEntity
class and there's no problem connecting to MongoDB. It's just the conversion is failing because of the missing class.
I have spring-data-mongodb
and mongo-java-driver
included in my pom.xml
. Is there anything I'm missing?
Upvotes: 4
Views: 5665
Reputation: 410
In case somebody else experiences the same issue, using a newer version of spring-data-mongodb
did the trick. For reference I had version 1.7.0.RELEASE
when I was experiencing the issue and updating to 1.8.2.RELEASE
solved it.
Upvotes: 1