无法使用Morphia连接到MongoDb

时间:2018-05-01 21:00:15

标签: java mongodb morphia

我试图使用Morphia使用Mongo v3.6.4从MongoDb.Im中读取。我已经添加了Morphia的依赖性:

<dependencies>
    <dependency>
        <groupId>org.mongodb.morphia</groupId>
        <artifactId>morphia</artifactId>
        <version>1.3.1</version>
    </dependency>
</dependencies>

现在我尝试连接到我的MongoDb:

final Morphia morphia = new Morphia();
morphia.mapPackage("org.mongodb.morphia.example");
final Datastore datastore = morphia.createDatastore(new MongoClient("localhost", 27017), "redmatter");

但我收到错误:

Exception in thread "pool-1-thread-1" java.lang.NoClassDefFoundError: org/bson/conversions/Bson
    at org.mongodb.morphia.Morphia.createDatastore(Morphia.java:90)
    at com.ie.pereda.server.GraphHandler.handle(GraphHandler.java:44)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79)
    at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:82)
    at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:675)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79)
    at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:647)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.ClassNotFoundException: org.bson.conversions.Bson
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

当我这样做时,我没有问题连接到同一个Mondo数据库:

MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
DB database = mongoClient.getDB("redmatter");

这没有问题。 Morphia的问题是什么?

修改

@ Victor的解决方案似乎解决了这个问题,但提出了一个新问题:

Exception in thread "pool-1-thread-1" java.lang.NoSuchMethodError: com.mongodb.MongoClient.getDatabase(Ljava/lang/String;)Lcom/mongodb/client/MongoDatabase;
    at org.mongodb.morphia.DatastoreImpl.<init>(DatastoreImpl.java:114)
    at org.mongodb.morphia.DatastoreImpl.<init>(DatastoreImpl.java:99)
    at org.mongodb.morphia.Morphia.createDatastore(Morphia.java:90)
    at com.ie.pereda.server.GraphHandler.handle(GraphHandler.java:44)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79)
    at sun.net.httpserver.AuthFilter.doFilter(AuthFilter.java:83)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:82)
    at sun.net.httpserver.ServerImpl$Exchange$LinkHandler.handle(ServerImpl.java:675)
    at com.sun.net.httpserver.Filter$Chain.doFilter(Filter.java:79)
    at sun.net.httpserver.ServerImpl$Exchange.run(ServerImpl.java:647)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)    

Mongo肯定是我以前用于连接作品的代码...

1 个答案:

答案 0 :(得分:1)

尝试添加maven依赖

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>bson</artifactId>
    <version>3.6.3</version>
</dependency>