apereo/cas · error · BeanCreationException

Unable to build a MongoDb client without any hosts/servers…

Error message

Unable to build a MongoDb client without any hosts/servers defined

What it means

MongoDbConnectionFactory builds a Mongo client settings object. If no clientUri is set, it falls back to splitting the `host` property on commas; if that yields no usable hosts it cannot construct any connection target, so it throws BeanCreationException during Mongo client bean creation (via mongoDbFactory).

Solutions

  1. Set cas.mongo.host, e.g. cas.mongo.host=localhost (comma-separate replicas: host1:27017,host2:27017)
  2. Or set a full connection string: cas.mongo.client-uri=mongodb://user:pass@host1:27017,host2:27017/db?replicaSet=rs0
  3. Verify the property source actually resolves (check env vars/profiles) so host is not blank at runtime

Example fix

// before
cas.mongo.host=
cas.mongo.port=27017

// after
cas.mongo.host=localhost:27017
# or
cas.mongo.client-uri=mongodb://localhost:27017/cas
Defensive patterns

Strategy: validation

Validate before calling

if (StringUtils.isBlank(mongo.getClientUri()) && StringUtils.isBlank(mongo.getHost())) {
    throw new IllegalStateException("Set cas.mongo.client-uri or cas.mongo.host before enabling Mongo modules");
}

Try / catch

try {
    client = MongoDbConnectionFactory.buildMongoDbClient(mongo);
} catch (BeanCreationException e) {
    logger.error("Mongo client construction failed: {}", e.getMessage());
    throw e;
}

Prevention

When it happens

Trigger: Configuring a Mongo-backed feature (tickets, services, authn) with cas.mongo.client-uri unset and cas.mongo.host empty/blank; buildMongoDbClient then produces a zero-length host array and fails.

Common situations: Forgetting to set host/uri for Mongo modules; YAML indentation mistakes leaving the host key empty; env placeholder ${MONGO_HOST} resolving to nothing; setting only the port while host is missing.

Understand the failure class

Background: "is required", "must be set", "missing required field": configuration validation errors across open-source libraries — this error's family across 36 libraries.

Related errors


AI-assisted analysis of apereo/cas@e7288fc434 (2026-09-08). Data as JSON: /api/errors/d470ffd636d1e142. Report an issue: GitHub.

Appendix: source

Thrown at support/cas-server-support-mongo-core/src/main/java/org/apereo/cas/mongo/MongoDbConnectionFactory.java:208

    }


    /**
     * Build mongo db client.
     *
     * @param mongo the mongo
     * @return the mongo client
     */
    public MongoClient buildMongoDbClient(final BaseMongoDbProperties mongo) {
        val settingsBuilder = MongoClientSettings.builder();

        if (StringUtils.isNotBlank(mongo.getClientUri())) {
            LOGGER.debug("Using MongoDb client URI [{}] to connect to MongoDb instance", mongo.getClientUri());
            settingsBuilder.applyConnectionString(new ConnectionString(mongo.getClientUri()));
        } else {
            val serverAddresses = mongo.getHost().split(",");
            if (serverAddresses.length == 0) {
                throw new BeanCreationException("Unable to build a MongoDb client without any hosts/servers defined");
            }
            val servers = new ArrayList<ServerAddress>();
            if (serverAddresses.length > 1) {
                LOGGER.debug("Multiple MongoDb server addresses are defined. Ignoring port [{}], "
                             + "assuming ports are defined as part of the address", mongo.getPort());
                Arrays.stream(serverAddresses)
                    .filter(StringUtils::isNotBlank)
                    .map(ServerAddress::new)
                    .forEach(servers::add);
            } else {
                val port = mongo.getPort() > 0 ? mongo.getPort() : DEFAULT_PORT;
                LOGGER.debug("Found single MongoDb server address [{}] using port [{}]", mongo.getHost(), port);
                val addr = new ServerAddress(mongo.getHost(), port);
                servers.add(addr);
            }
            settingsBuilder.applyToClusterSettings(builder -> {
                builder.hosts(servers);
                if (StringUtils.isNotBlank(mongo.getReplicaSet())) {

View on GitHub (pinned to e7288fc434)