我有很多代码可以从mongodb数据库中检索文档。它在普通的java项目中工作正常(使用public static void main),但是当我在Sprig-MVC应用程序中尝试相同的代码块时,我得到了
HTTP状态500 - 请求处理失败;嵌套异常是com.mongodb.MongoTimeoutException:在等待与ReadPreferenceServerSelector {readPreference = primary}匹配的服务器后30000 ms后超时。集群状态的客户端视图是{type = UNKNOWN,servers = [{address = localhost:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoException:java.lang.NoSuchMethodError:org.bson.BsonBinaryReader。 (Lorg / bson / io / BsonInput;)V},由{java.lang.NoSuchMethodError:org.bson.BsonBinaryReader。(Lorg / bson / io / BsonInput;)V}}引起}" ***异常消息..
以下是日志:
servlet [mvc-dispatcher]的Servlet.service()在路径[/ temp]的上下文中引发异常[请求处理失败;嵌套异常是com.mongodb.MongoTimeoutException:在等待与ReadPreferenceServerSelector {readPreference = primary}匹配的服务器后30000 ms后超时。集群状态的客户端视图是{type = UNKNOWN,servers = [{address = localhost:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoException:java.lang.NoSuchMethodError:org.bson.BsonBinaryReader。 (Lorg / bson / io / BsonInput;)V},由{java.lang.NoSuchMethodError:org.bson.BsonBinaryReader。(Lorg / bson / io / BsonInput;)V}}]]引起的 com.mongodb.MongoTimeoutException:在等待与ReadPreferenceServerSelector {readPreference = primary}匹配的服务器30000 ms后超时。集群状态的客户端视图是{type = UNKNOWN,servers = [{address = localhost:27017,type = UNKNOWN,state = CONNECTING,exception = {com.mongodb.MongoException:java.lang.NoSuchMethodError:org.bson.BsonBinaryReader。 (Lorg / bson / io / BsonInput;)V},由{java.lang.NoSuchMethodError:org.bson.BsonBinaryReader。(Lorg / bson / io / BsonInput;)V}}]引起 at com.mongodb.connection.BaseCluster.createTimeoutException(BaseCluster.java:370) 在com.mongodb.connection.BaseCluster.selectServer(BaseCluster.java:101) 在com.mongodb.binding.ClusterBinding $ ClusterBindingConnectionSource。(ClusterBinding.java:75) 在com.mongodb.binding.ClusterBinding $ ClusterBindingConnectionSource。(ClusterBinding.java:71) 在com.mongodb.binding.ClusterBinding.getReadConnectionSource(ClusterBinding.java:63) 在com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:166) 在com.mongodb.operation.FindOperation.execute(FindOperation.java:394) 在com.mongodb.operation.FindOperation.execute(FindOperation.java:57) 在com.mongodb.Mongo.execute(Mongo.java:738) 在com.mongodb.Mongo $ 2.execute(Mongo.java:725) 在com.mongodb.FindIterableImpl $ FindOperationIterable.first(FindIterableImpl.java:197) 在com.mongodb.FindIterableImpl.first(FindIterableImpl.java:140)
以下是java代码:
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import pojos.LoginCredentials;
public class LoginDAO {
private LoginCredentials logindata;
public LoginDAO(LoginCredentials logindata){
this.logindata=logindata;
}
public String retrievedPassword(){
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
MongoDatabase db = mongoClient.getDatabase("progressreport");
MongoCollection<Document> coll = db.getCollection("studentprofile");
Document cursor = coll.find().first();
System.out.println(cursor.toString());
return cursor.getString("password");
}
}