4

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?

0

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you miss spring-data-commons-core jar

4 Comments

I actually tried including it in my pom.xml, but it didn't help. Still, I believe it should be automatically downloaded as it's obviously a dependency.
@VinoDang I have come across with that site before, but unfortunately it didn't help either.
could you post your pom.xml?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.