Can't connect to Atlas from java

I can connect using the mongosh, but have seen various exceptions when trying to connect with a java client directly. The most recent one I can’t get past when using a direct copy paste from the docs is the following…

  ServerApi api = ServerApi.builder().version(ServerApiVersion.V1).build();
  String uri = "mongodb+srv://retail_user:***************@cluster0.********";

  MongoClientSettings settings = MongoClientSettings.builder()
          .applyConnectionString(new ConnectionString(uri))

  MongoClient mongoClient = MongoClients.create(settings);

The exception thrown is

The method serverApi(ServerApi) is undefined for the type MongoClientSettings.Builder

I copy pasted the code above, using the 4.7 driver, from

I pasted the same code into my editor and it compiles fine with 4.7. You should double-check that you’re actually using the right driver version, as that method was added all the way back in the 4.3 release.


1 Like

Thanks, Jeff. You were correct, I had an old jar in the tomcat lib directory that had an old version that in fact did not have the noted method. I can compile, but am getting an authn error for the same thing that again, works with mongosh. This is a simple as it gets, and I have tried various versions of embedding the user/pwd in the URL, using a MongoCredential object, but all return the same thing.

com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=SCRAM-SHA-1, userName='retail_user', source='Cluster0', password=<hidden>, mechanismProperties=<hidden>}

Let me know if this should be a new question.

import com.mongodb.client.*;
import com.mongodb.ServerApi;
import com.mongodb.ServerApiVersion;
import com.mongodb.MongoClientSettings;
import com.mongodb.MongoCredential;
import com.mongodb.ConnectionString;
import org.bson.*;

public class a{
  public static void main (String args[]) {
    try {

      ServerApi api = ServerApi.builder().version(ServerApiVersion.V1).build();
      //mongosh "mongodb+srv://cluster0.********" --username retail-user -p ************
      String uri = "mongodb+srv://retail_user:*************@cluster0.*********";
      uri = "mongodb+srv://cluster0.*********";

      MongoCredential credential = MongoCredential.createCredential(
                    "retail_user", "Cluster0",

      MongoClientSettings settings = MongoClientSettings.builder()
              .applyConnectionString(new ConnectionString(uri))

      MongoClient mongoClient = MongoClients.create(settings);

      MongoDatabase db = mongoClient.getDatabase("Cluster0");
      MongoCollection<Document> collection = db.getCollection("inventory");
      for (Document doc : collection.find()) {
        System.out.println(doc.toJson() + "<br>");
    catch(Exception e) {

Users are typically in the “admin” database, so you would want this instead:

      MongoCredential credential = MongoCredential.createCredential(
                    "retail_user", "admin",

Or you can use your original uri that contained the credential embedded in it. That will default to the “admin” database as the credential source.

Thanks again, Jeff, I appreciate the eyes on it. That’s interesting, as I would also expect it to work, but I keep getting an authn error with both the embedded URI and the credential object. I used the starter python example, and that works fine, so something is amuck in java, but I don’t know what. I am a partner of Mongo’s with a tech talk tomorrow and wanted to use MongoDB as a sink in a demo, but the java piece has taken too many cycles. I am sure it is me and something simple I am missing, but I will just use python with Flask for a web part of the demo. This works…

>>> from pymongo import MongoClient
>>> CONNECTION_STRING = "mongodb+srv://retail-user:*************@cluster0.********"
>>> client = MongoClient(CONNECTION_STRING)
>>> dbname = client['Cluster0']
>>> collection_name = dbname["inventory"]
>>> item_details = collection_name.find_one()
>>> for item in item_details:
...    print(item)

Happy to assist further but I understand that you have time constraints.

Good luck in the demo!

It was me. An underscore rather than a dash in the username. Doh! Thanks again!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.