1. Home
  2. Docs
  3. Java Micronaut Examples
  4. Java Micronaut REST Server with PostgreSQL

Java Micronaut REST Server with PostgreSQL

? 45 minutes

What you’ll learn

How to setup your application for : 

  • connecting to PostgreSQL database,
  • getting data from REST API, 
  • providing data to REST API. 

In this tutorial, we will create a simple java component with Java Micronaut Data scaffolder with connection to PostgreSQL database storage. We want to expose single REST endpoint for getting basic client data information, creating a microservice CRUD layer above DB storage.

Project source

This example project can be cloned from: [email protected]:innobank/client-data-db.git

Before you begin 


Open your IDE, import created component and start coding:

  • Define jpa entity Client. This simple table will store basic client data:
    • Generate getters and setters with your IDE
package io.codenow.client.data.db.service.repository.entity;

import java.time.LocalDate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

public class Client {
    private Long id;

    private String username;
    private String firstname;
    private String surname;
    private LocalDate birthdate;
  • Create new ClientRepository, which is basic CRUD interface for micronaut data DB access:
package io.codenow.client.data.db.service.repository;

import io.codenow.client.data.db.service.repository.entity.Client;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.repository.CrudRepository;
import io.reactivex.Maybe;

public interface ClientRepository extends CrudRepository<Client, Long> {
	Maybe<Client> findByUsername(String username);
package io.codenow.client.data.db.service.controller;

import javax.inject.Inject;
import javax.validation.constraints.NotNull;

import io.codenow.client.data.db.service.repository.ClientRepository;
import io.codenow.client.data.db.service.repository.entity.Client;
import io.micronaut.http.annotation.Consumes;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.micronaut.http.annotation.PathVariable;
import io.micronaut.http.annotation.Produces;
import io.micronaut.validation.Validated;
import io.reactivex.Flowable;
import io.reactivex.Maybe;

 * ClientDataController.
public class ClientDataController {

	private final ClientRepository clientRepository;

	public ClientDataController(ClientRepository bookRepository) {
    	this.clientRepository = bookRepository;

	public Flowable<Client> listClients() {
    	return Flowable.fromIterable(clientRepository.findAll());

	public Maybe<Client> getClient(@PathVariable @NotNull String username) {
    	return clientRepository.findByUsername(username);


  • Next prepare database configuration:
    • Go to PgAdmin console (http://localhost:5050 if using compose-postgre from our Local development manual) and create new db client-db with scheme client-data.
    • Add maven dependency to your pom.xml
  • Now change configuration in config/application.yaml:
  • Fill {db user} and {db password} according to your configuration
  • Make sure you follow yaml syntax (especially whitespaces)

    url: jdbc:postgresql://localhost:5432/client-db?currentSchema=client-data
    driverClassName: org.postgresql.Driver
    username: {db user}
    password: {db password}
    schema-generate: CREATE
    dialect: POSTGRES
  • Do not forget to change swagger.yaml. Check it in example project: 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

What’s next?

If your code works in 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. For more information see Application Deployment and Monitoring, just make sure to change application.yaml properties from the local to the production setup.

Was this article helpful to you? Yes 1 No