Dropwizard makes spinning up Java REST services simple—even more so when paired with a reliable database like MySQL. In this guide, you'll discover how to configure MySQL for both local development and cloud-ready production. We’ll use Docker for local setup, Kubernetes for production, and split your configuration into
config-dev.yml
and
config-prod.yml
to keep things clean and secure.
1. Add Dependencies in pom.xml
Make sure your project includes the essentials:
<dependencies>
<!-- Dropwizard core framework -->
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
</dependency>
<!-- MySQL JDBC driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.version}</version>
</dependency>
<!-- Kubernetes client (optional) -->
<dependency>
<groupId>io.fabric8</groupId>
<artifactId>kubernetes-client</artifactId>
<version>${fabric8.version}</version>
</dependency>
</dependencies>
You’ll have Dropwizard for your service logic, MySQL connectivity, and optional Kubernetes support for cloud deployments.
2. Run MySQL with Docker for Local Dev
Create a
docker-compose.yml
:
version: '3'
services:
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: devdb
MYSQL_USER: devuser
MYSQL_PASSWORD: devpass
ports:
- "3306:3306"
Launch your local MySQL instance:
docker-compose up -d
You now have a local MySQL server at
localhost:3306
, database
devdb
, and test credentials.
3. Local Configuration: config-dev.yml
Create a YAML file for dev mode:
server:
applicationConnectors:
- type: http
port: 8080
database:
driverClass: com.mysql.cj.jdbc.Driver
user: devuser
password: devpass
url: jdbc:mysql://localhost:3306/devdb?useSSL=false&serverTimezone=UTC
validationQuery: SELECT 1
Define your config POJO:
public class DbConfig {
@JsonProperty public String driverClass;
@JsonProperty public String user;
@JsonProperty public String password;
@JsonProperty public String url;
@JsonProperty public String validationQuery;
}
public class AppConfig extends Configuration {
@JsonProperty("database")
public DbConfig database = new DbConfig();
}
Create the data source in your
Application
subclass:
ManagedDataSource ds = database.build(environment.metrics(), "mysql");
environment.lifecycle().manage(ds);
Run locally:
java -jar target/myapp.jar server config-dev.yml
Your Dropwizard app is now connected to MySQL—ready for development.
4. Prepare Kubernetes for Production
In production, credentials and connection details belong in ConfigMaps and Secrets.
Run:
kubectl create configmap mysql-config \
--from-literal=DB_URL=jdbc:mysql://mysql-service:3306/proddb?useSSL=false&serverTimezone=UTC \
--from-literal=DB_DRIVER=com.mysql.cj.jdbc.Driver \
--from-literal=DB_VALIDATION_QUERY=SELECT 1
kubectl create secret generic mysql-secret \
--from-literal=DB_USER=produser \
--from-literal=DB_PASSWORD=prodpass
This will feed your service securely at runtime.
5. Production Configuration: config-prod.yml
Your production setup will reference env vars:
server:
applicationConnectors:
- type: http
port: 8080
database:
driverClass: ${DB_DRIVER}
user: ${DB_USER}
password: ${DB_PASSWORD}
url: ${DB_URL}
validationQuery: ${DB_VALIDATION_QUERY}
6. Kubernetes Deployment Example
Include environment variables in your spec:
env:
- name: DB_URL
valueFrom:
configMapKeyRef:
name: mysql-config
key: DB_URL
- name: DB_DRIVER
valueFrom:
configMapKeyRef:
name: mysql-config
key: DB_DRIVER
- name: DB_VALIDATION_QUERY
valueFrom:
configMapKeyRef:
name: mysql-config
key: DB_VALIDATION_QUERY
- name: DB_USER
valueFrom:
secretKeyRef:
name: mysql-secret
key: DB_USER
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: DB_PASSWORD
Run your app in production:
java -jar myapp.jar server config-prod.yml
Your application will now connect securely to a cloud-hosted MySQL instance.
Why This Approach Works
- Docker + YAML = fast local setup
- Kubernetes integration = secure credentials and configs
- Separate config files = clean environment segregation
- Scalable & secure—from dev to production
This setup shows how easy it is to build a Dropwizard service backed by MySQL. Whether you're iterating locally with Docker or deploying to a Kubernetes cluster, this environment-aware approach ensures smooth transitions, secure configurations, and better maintainability. Your service is well-equipped to run in production with confidence and clarity.
image quote pre code