Dropwizard offers a solid foundation for building RESTful Java applications, and MongoDB makes working with document data flexible and fast. We’ll walk through setting up MongoDB in Dropwizard:
- Local development with Docker and
config-dev.yml
- Production deployment on Kubernetes using
config-prod.yml
, ConfigMaps, and Secrets
- A minimal Maven setup with Dropwizard, MongoDB, and Kubernetes support
Let’s get started!
1. Add Dependencies in pom.xml
First, add these essential libraries so your app can connect to MongoDB and optionally integrate with Kubernetes:
<dependencies>
<!-- Dropwizard core -->
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<!-- MongoDB Java Driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.0</version>
</dependency>
<!-- Fabric8 Kubernetes client (optional) -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
These enable Dropwizard essentials, secure MongoDB access, and Kubernetes support.
2. Run MongoDB Locally with Docker
For local development, spin up a MongoDB container:
version: '3.8'
services:
mongo:
image: mongo:5.0
ports:
- "27017:27017"
volumes:
- mongo-data:/data/db
volumes:
mongo-data:
Run:
docker-compose up -d
Your MongoDB instance will then be available at
mongodb://localhost:27017
.
3. Configure Dev Setup: config-dev.yml
Create a configuration file for development:
server:
applicationConnectors:
- type: http
port: 8080
mongo:
host: localhost
port: 27017
database: devdb
You’ll need a corresponding config POJO:
public class MongoConfig {
@JsonProperty public String host;
@JsonProperty public int port;
@JsonProperty public String database;
}
public class AppConfig extends Configuration {
@JsonProperty("mongo")
public MongoConfig mongo = new MongoConfig();
}
Initialize your Mongo client in the
Application
class:
String uri = String.format("mongodb://%s:%d", config.mongo.host, config.mongo.port);
MongoClient mongoClient = new MongoClient(new MongoClientURI(uri));
DB db = mongoClient.getDB(config.mongo.database);
Run your app locally:
java -jar target/myapp.jar server config-dev.yml
4. Prepare Kubernetes for Production
Keep production settings secure using Kubernetes resources:
kubectl create configmap mongo-config \
--from-literal=MONGO_HOST=mongo-service \
--from-literal=MONGO_PORT=27017 \
--from-literal=MONGO_DATABASE=proddb
kubectl create secret generic mongo-secret \
--from-literal=MONGO_USER=produser \
--from-literal=MONGO_PASSWORD=prodpass
This creates ConfigMap and Secret for audi configuration and credentials.
5. Define Production Config: config-prod.yml
Use environment-based variables so your app picks up prod settings at runtime:
server:
applicationConnectors:
- type: http
port: 8080
mongo:
host: ${MONGO_HOST}
port: ${MONGO_PORT}
database: ${MONGO_DATABASE}
user: ${MONGO_USER}
password: ${MONGO_PASSWORD}
Dropwizard will automatically inject these values.
6. Kubernetes Deployment Snippet
Ensure your Kubernetes manifest references the ConfigMap and Secret:
env:
- name: MONGO_HOST
valueFrom:
configMapKeyRef:
name: mongo-config
key: MONGO_HOST
- name: MONGO_PORT
valueFrom:
configMapKeyRef:
name: mongo-config
key: MONGO_PORT
- name: MONGO_DATABASE
valueFrom:
configMapKeyRef:
name: mongo-config
key: MONGO_DATABASE
- name: MONGO_USER
valueFrom:
secretKeyRef:
name: mongo-secret
key: MONGO_USER
- name: MONGO_PASSWORD
valueFrom:
secretKeyRef:
name: mongo-secret
key: MONGO_PASSWORD
Start your app:
java -jar myapp.jar server config-prod.yml
7. Why This Pattern Works
- Rapid local builds with Docker and dev config
- Secure and scalable production using Kubernetes ConfigMaps and Secrets
- Clear separation between development and production environments
- Easily extendable structure to handle users, retries, indexes, or replicas
Following this setup, you get a fast local development loop, production readiness, and clear environment boundaries. Your Dropwizard service is now seamlessly integrated with MongoDB—locally via Docker, and securely in the cloud via Kubernetes. It’s a clean, maintainable, and robust foundation for real-world applications.
image quote pre code