Java Micronaut REST Server with CockroachDB
đź•“ 45 minutes
#
What you’ll learnHow to setup your application for :
- connecting to CockroachDB database,
- getting data from REST API,
- providing data to REST API.
In this tutorial, we will create a simple java component with Java Micronaut scaffolder with a connection to CockroachDB database storage. We want to expose a single REST endpoint for getting the basic client data information, creating a microservice CRUD layer above the DB storage.

#
Project sourceThis example project can be cloned from: http://gitlab.factory.codenow-control.codenow.com/public-docs/java-micronaut-demo/rest-server-with-cockroachdb.git
#
Prerequisites- Prepare your local development environment for CodeNOW with Micronaut.
- Follow the tutorial instructions in the Java Micronaut Local Development tutorial.
- Run CockroachDB locally.
- You can run CockroachDB directly or using docker compose.
- The configuration file for docker-compose can be downloaded from this link: https://kb.objectrocket.com/cockroachdb/docker-compose-and-cockroachdb-1151
- Create a new component
- For details see the section Prerequisites of the Java Micronaut Local Development tutorial.
#
StepsOpen your IDE, import the created component and start coding:
First, add maven dependency to your pom.xml
Then define the jpa entity Client. This simple table will store basic client data:
- Generate getters and setters with your IDE
Create a new ClientRepository, which is a basic CRUD interface for micronaut data DB access:
Create a new controller and put all the parts together
- For more details about Micronaut controller, see: https://docs.micronaut.io/latest/guide/index.html#httpServer
Next prepare the database configuration:
- If you are using docker-compose or regular docker, then go to the cockroach sql terminal and create a new database.
- You can use this link for help: https://kb.objectrocket.com/cockroachdb/docker-compose-and-cockroachdb-1151
- Then create a new table with the name "client" inside your new database and fill it with values.
- If you are using docker-compose or regular docker, then go to the cockroach sql terminal and create a new database.
Now change the configuration in config/application.yaml:
Fill {db user} and {db password} according to your configuration
Make sure you follow yaml syntax (especially whitespaces)
Do not forget to change the swagger.yaml. Add these lines into your src/main/resources/META-INF/swagger/swagger.yaml :
Try to build and run the application in your IDE. After startup, you should be able to access your new controller’s swagger: http://localhost:8080/swagger/index.html
- For correct setup, check the Readme.md in the project root or see the tutorial Java Micronaut Local Development

#
Deploy to CodeNOWIf your code works in the local development, you are ready to push your changes to GIT and try to build and deploy your new component version to the CodeNOW environment.
- Check the Get New CockroachDB user manual to get CodeNOW managed component properties.
- Make sure to change the application.yaml properties from the local to the production setup.
- For more information about application deployment see Application Deployment and Deployment Monitoring tutorials.
#
What’s next?See the tutorial Micronaut REST server with Redis and Kafka.