Kotlin + Quarkus + Hibernate - Build REST CRUD API example

Hello everyone, today we will learn how to build RESTful API using KotlinQuarkus, and Hibernate with the help of one simple example. You can download the source code from our Github repository.

Quarkus is a Java framework designed to run within containers. Fixating on expeditious start-up times and low memory utilization makes it more felicitous to run within container orchestration platforms like Kubernetes. Quarkus supports many industry-standard libraries such as Hibernate, Kubernetes, RESTEasy, Eclipse MicroProfile, and more...

It uses JAX-RS for the REST endpoints, JPA to preserve data models, and CDI for dependency injections. 

Let's start to build RESTful API with QUARKUS

 Technologies used:

  • Quarkus 2.2.3.Final
  • Hibernate 5.5.7.Final
  • H2 Database
  • Kotlin

Project Structure:

Gradle Build(build.gradle.kts):

import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.5.31"

repositories {

val quarkusPlatformGroupId: String by project
val quarkusPlatformArtifactId: String by project
val quarkusPlatformVersion: String by project

dependencies {


group = "com.knf.dev.demo"
version = "1.0.0-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11

tasks.withType<JavaCompile> {
options.encoding = "UTF-8"
val compileKotlin: KotlinCompile by tasks
compileKotlin.kotlinOptions {
jvmTarget = "11"
val compileTestKotlin: KotlinCompile by tasks
compileTestKotlin.kotlinOptions {
jvmTarget = "11"


# datasource configuration
quarkus.datasource.db-kind = h2
quarkus.datasource.username = sa
# quarkus.datasource.password =
quarkus.datasource.jdbc.url = jdbc:h2:mem:test

# drop and create the database at startup
#quarkus.hibernate-orm.sql-load-script = data.sql

Entity class(User.kt) - ORM

package com.knf.dev.demo.entity

import java.io.Serializable
import javax.persistence.*

@Table(name = "users")
class User : Serializable {
@GeneratedValue(strategy = GenerationType.AUTO)
var id: Long? = null
var firstName: String? = null
var lastName: String? = null
var emailId: String? = null

constructor(id: Long?, firstName: String?,
         lastName: String?, emailId: String?) : super() {

this.id = id
this.firstName = firstName
this.lastName = lastName
this.emailId = emailId
constructor() : super() {}


package com.knf.dev.demo.service
import com.knf.dev.demo.entity.User
import javax.inject.Inject
import javax.inject.Singleton
import javax.persistence.EntityManager
import javax.transaction.Transactional

class UserService {
var entityManager: EntityManager? = null

fun getUsers(): kotlin.collections.List<User?>? {
return entityManager!!.
createQuery("SELECT c FROM User c").
resultList as List<User?>?

fun getUser(id: Long?): User {
return entityManager!!.find(User::class.java, id)

fun addUser(user: User?): User? {
return user

fun updateUser(id: Long?, user: User) {
val userToUpdate: User = entityManager!!.
find(User::class.java, id)
if (null != userToUpdate) {
userToUpdate.firstName = user.firstName
userToUpdate.lastName = user.lastName
userToUpdate.emailId = user.emailId
} else {
throw RuntimeException("No such user available")

fun deleteUser(id: Long?) {
val user: User = getUser(id)


package com.knf.dev.demo.controller
import com.knf.dev.demo.entity.User
import com.knf.dev.demo.service.UserService
import javax.inject.Inject
import javax.ws.rs.*
import javax.ws.rs.core.MediaType

class UserController {
var userResource: UserService? = null

fun getUsers(): kotlin.collections.List<User?>? {
return userResource?.getUsers()

fun getUser(@PathParam("id") id: Long?): User? {
return userResource?.getUser(id)

fun updateUser(@PathParam("id") id: Long?, user: User?) {
userResource?.updateUser(id, user!!)

fun addUser(user: User?): User? {
return userResource?.addUser(user)

fun deleteUser(@PathParam("id") id: Long?) {

Verify REST APIs

Build application jar file:
gradle build

Start the application: 
java -jar quarkus-run.jar

1. Add user

 2. Update user

3. Delete user


4. Get all users


5. Get single user

More Quarkus Related topics,

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Spring Boot + Mockito simple application with 100% code coverage

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

Spring boot video streaming example-HTML5

ReactJS, Spring Boot JWT Authentication Example

Java - Blowfish Encryption and decryption Example

Custom Exception Handling in Quarkus REST API

Spring Boot + OpenCSV Export Data to CSV Example