When building a Java web app with Struts, MongoDB is a fantastic choice if you're working with JSON-like documents, schemas that evolve, or need fast reads and writes. In this guide, we'll cover how to integrate MongoDB into a Struts application—using Docker for local development, Kubernetes for production, and clear separation of configuration for each environment.
1. Add Dependencies in pom.xml
Start by including the necessary libraries for web flow, MongoDB connectivity, and optional Kubernetes support:
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- MongoDB Java Driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${mongo.driver.version}</version>
</dependency>
<!-- Kubernetes client (optional) -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
With these in place, your Struts app can connect to MongoDB and interact with Kubernetes environment variables when needed.
2. Run MongoDB Locally via Docker
Use Docker Compose for easy setup in development:
version: '3'
services:
mongo:
image: mongo:6.0
ports:
- "27017:27017"
Start it up:
docker-compose up -d
This runs MongoDB on
localhost:27017
.
3. Create Development Config: mongo-dev.properties
Add the following in
src/main/resources
:
mongo.uri=mongodb://localhost:27017
mongo.database=devdb
In your Struts utility class or connector:
Properties props = new Properties();
props.load(getClass().getResourceAsStream("/mongo-dev.properties"));
MongoClient client = MongoClients.create(props.getProperty("mongo.uri"));
MongoDatabase db = client.getDatabase(props.getProperty("mongo.database"));
MongoCollection<Document> col = db.getCollection("samples");
You're now connected and ready to query or insert documents.
4. Prepare Kubernetes Config for Production
In Kubernetes, use ConfigMaps and Secrets for production settings:
kubectl create configmap mongo-config \
--from-literal=MONGO_URI=mongodb://mongo-service:27017 \
--from-literal=MONGO_DATABASE=proddb
kubectl create secret generic mongo-secret \
--from-literal=MONGO_USER=produser \
--from-literal=MONGO_PASSWORD=prodpass
5. Create Production Config: mongo-prod.properties
Place this file into your app’s resource directory:
mongo.uri=${MONGO_URI}
mongo.database=${MONGO_DATABASE}
mongo.user=${MONGO_USER}
mongo.password=${MONGO_PASSWORD}
No code changes are required—environment variables handle the differences.
6. Kubernetes Deployment Snippet
Include these environment variables in your Deployment:
env:
- name: MONGO_URI
valueFrom:
configMapKeyRef:
name: mongo-config
key: MONGO_URI
- 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
This ensures your application uses production credentials securely at startup.
7. Example Struts Action for MongoDB Access
Here’s a simple action to test MongoDB in Struts:
public class MongoAction extends ActionSupport {
public String execute() {
Properties props = new Properties();
String propsFile = System.getenv().containsKey("MONGO_URI") ? "mongo-prod.properties" : "mongo-dev.properties";
props.load(getClass().getResourceAsStream("/" + propsFile));
MongoClientSettings.Builder builder = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(props.getProperty("mongo.uri")));
if (props.getProperty("mongo.user") != null) {
builder.credential(MongoCredential.createCredential(
props.getProperty("mongo.user"), props.getProperty("mongo.database"),
props.getProperty("mongo.password").toCharArray()));
}
MongoClient client = MongoClients.create(builder.build());
MongoDatabase db = client.getDatabase(props.getProperty("mongo.database"));
long count = db.getCollection("samples").countDocuments();
addActionMessage("Found " + count + " documents in MongoDB!");
return SUCCESS;
}
}
This works identically in dev and prod—only the properties file changes.
Why This Setup Is Solid
- Fast local development: Docker + properties file = instant setup
- Secure production: Credentials live in Kubernetes Secrets
- Minimal code changes: Struts logic stays the same across environments
- Clean environment separation: Different config files per environment reduce errors
With Docker for local dev, Kubernetes for production, and simple properties file management, your Struts application is fully equipped to work with MongoDB across all environments. Enjoy building flexible, scalable, and document-driven Java web applications!
image quote pre code