public void MongoDBClient(String user, String pwd, String dbName, String collectionName) {
MongoClientURI uri = new MongoClientURI("mongodb+srv://" + user + ":" + pwd + "@cluster0.mff6p.mongodb.net/"
+ dbName + "?retryWrites=true&w=majority&connectTimeoutMS=30000&socketTimeoutMS=30000");
try (MongoClient mongoClient = new MongoClient(uri)) {
MongoDatabase database = mongoClient.getDatabase(dbName);
MongoCollection<Document> collection = database.getCollection(collectionName);
Document query = new Document("_id", new ObjectId("5f05e46281048f54ac98c455"));
Document result = collection.find(query).iterator().next();
System.out.println(result);
System.out.println("Test3: " + result.getString("Cluster"));
}
}
Получение исключения в приведенном выше коде -
INFO: Exception in monitor thread while connecting to server cluster0-shard-00-01.mff6p.mongodb.net:27017
com.mongodb.MongoSocketReadException: Prematurely reached end of stream
at com.mongodb.internal.connection.SocketStream.read(SocketStream.java:112)
at com.mongodb.internal.connection.InternalStreamConnection.receiveResponseBuffers(InternalStreamConnection.java:580)
at com.mongodb.internal.connection.InternalStreamConnection.receiveMessage(InternalStreamConnection.java:445)
at com.mongodb.internal.connection.InternalStreamConnection.receiveCommandMessageResponse(InternalStreamConnection.java:299)
at com.mongodb.internal.connection.InternalStreamConnection.sendAndReceive(InternalStreamConnection.java:259)
at com.mongodb.internal.connection.CommandHelper.sendAndReceive(CommandHelper.java:83)
at com.mongodb.internal.connection.CommandHelper.executeCommand(CommandHelper.java:33)
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initializeConnectionDescription(InternalStreamConnectionInitializer.java:105)
at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:62)
at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:129)
at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117)
at java.base/java.lang.Thread.run(Thread.java:834)
Кто-нибудь может мне помочь, я думаю, что есть проблема с URL-адресом строки подключения, MongoClientURI ...