Build REST CRUD API's with Kotlin, Spring, Spring Data JPA, and RDBMS

In this article, we will show you how to develop a  REST-style web service with Kotlin, Spring Boot, H2DB.

A quick overview of Kotlin, Spring Boot, and H2DB 


Kotlin is a statically typed, general-purpose programming language targeting the Java platform. Kotlin is concise, safe, pragmatic, and fixated on interoperability with Java code. It can be used virtually everywhere Java is utilized today: for server-side development, Android apps, and much more. Kotlin works great with all subsisting Java libraries and frameworks and runs with the same level of performance as Java.


H2DB is a relational database management system written in Java. It can be embedded in Java applications or run in client-server mode.

Spring Boot

Spring boot to develop REST web services and microservices. Spring Boot has taken the Spring framework to the next level. It has drastically reduced the configuration and setup time required for spring projects. We can set up a project with almost zero configuration and start building the things that actually matter to your application.

Technologies used :

  • Spring Boot 2.3.7.RELEASE
  • Spring  5.2.12.RELEASE
  • Kotlin 2.11.3
  • Hibernate 5.4.25.Final
  • H2DB
  • Maven 3
  • Java 8

After completing this tutorial what we will build? 

We will build REST API  CRUD features: 

  1. GET - Fetch all User       :     /api/v1/users
  2. GET - Get User by ID     :     /api/v1/users/{id} 
  3. POST - Create User         :     /api/v1/users 
  4. PUT - Edit User Details   :     /api/v1/users/{id} 
  5. DELETE - Delete User    :     /api/v1/users/{id}

Project Directory



<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
<relativePath/> <!-- lookup parent from repository -->
<description>Demo project for Spring Boot</description>






Creating the Entity[User.kt]


import javax.persistence.*

@Table(name = "user")
data class User(

@GeneratedValue(strategy = GenerationType.AUTO)
var id: Long,
val firstName: String,
val lastName: String,
val emailId: String


Creating the Repository[UserRepository.kt]


import org.springframework.stereotype.Repository

interface UserRepository : JpaRepository<User, Long>

Creating the controller End-points[UserController.kt]


import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

class UserController(private val userRepository: UserRepository) {

fun getAllUsers(): List<User> =

fun createNewUser(@RequestBody user: User): User =

fun getUserById(@PathVariable(value = "id") userId: Long):
ResponseEntity<User> {
return userRepository.findById(userId).map { usr ->

fun updateUserById(@PathVariable(value = "id") userId: Long,
@RequestBody newUser: User):
ResponseEntity<User> {

return userRepository.findById(userId).map { existingUser ->
val updatedEmployee: User = existingUser
.copy(firstName = newUser.firstName, lastName =
newUser.lastName, emailId = newUser.emailId)


fun deleteUserById(@PathVariable(value = "id") userId: Long):
ResponseEntity<Void> {
return userRepository.findById(userId).map { usr ->




import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication

class KotlinSpringbH2DBRestfulApiApplication

fun main(args: Array<String>) {


$ mvn spring-boot:run

Testing API's using Postman

Create an User

Fetch all User

Update User

Delete User By Id

Get User By Id

Download  source code

More Kotlin Examples,

Popular posts from this blog

Spring boot video streaming example-HTML5

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

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Java - Blowfish Encryption and decryption Example