Build REST CRUD API's with Kotlin, Spring, and MongoDB

In this article, we will show you how to develop a  REST-style web service with Kotlin, Spring Boot, MongoDB.
A quick overview of Kotlin, Spring Boot, and MongoDB  

Kotlin:

It’s a modern 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. 

MongoDB:

MongoDB is a document database built on a scale-out architecture that has to propagate with developers of all kinds who are building scalable applications utilizing agile methodologies.
MongoDB was built for people who are building internet and business applications who need to evolve expeditiously and scale elegantly. If you are doing that, you should consider MongoDB.

Companies and development teams of all sizes use MongoDB because:
  • The document data model is a potent way to store and retrieve data that sanctions developers to move expeditiously.
  • MongoDB’s horizontal, scale-out architecture can fortify astronomically immense volumes of both data and traffic.
  • MongoDB has a great utilizer experience for developers who can install MongoDB and commence inscribing code immediately. 

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
  • MongoDB
  • Maven 3
  • Java 8

After completing this tutorial what we will build? 

We will build REST API  CRUD features: 

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

Project Directory:



Maven[pom.xml]:

Maven is a powerful project management tool that is based on POM (project object model). It is used for project build, dependency and documentation. 
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev</groupId>
<artifactId>springboot_kotlin_crud</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot_kotlin_crud</name>
<description>Demo project for Spring Boot</description>

<properties>
<java.version>1.8</java.version>
<kotlin.version>1.3.72</kotlin.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-reflect</artifactId>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>
${project.basedir}/src/test/kotlin
</testSourceDirectory>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<configuration>
<args>
<arg>-Xjsr305=strict</arg>
</args>
<compilerPlugins>
<plugin>spring</plugin>
</compilerPlugins>
</configuration>
<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-allopen</artifactId>
<version>${kotlin.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>

Creating the Document:

The employee model here is the POJO, We use the annotation @Document to set the collection name that will be used by the model. If the collection doesn’t exist, MongoDB will create it.
package com.knf.dev.model

import org.bson.types.ObjectId
import org.springframework.data.annotation.Id
import org.springframework.data.mongodb.core.mapping.Document
import org.springframework.web.bind.annotation.CrossOrigin

@Document(collection = "employees")
data class Employee(
@Id
var id: String? = ObjectId().toHexString(),
val firstName: String,
val lastName: String,
val emailId: String
)


Creating the Repository:

The API implementation happens in the repository. It acts as a link between the model and the database and has all the methods for CRUD operations.
package com.knf.dev.repository

import com.knf.dev.model.Employee
import org.springframework.data.mongodb.repository.MongoRepository
import org.springframework.stereotype.Repository

@Repository
interface EmployeeRepository : MongoRepository<Employee, String>


Creating the Rest Controller:

Rest Controller annotation is mainly used for building restful web services using Spring MVC. The class annotated with @RestController is called Rest Controller. The class annotated with @RestController annotation returns JSON response in all the methods.
package com.knf.dev.controller

import com.knf.dev.model.Employee
import com.knf.dev.repository.EmployeeRepository
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*
import java.util.*

@RestController
@RequestMapping("/api/v1/")
class EmployeeController(private val employeeRepository: EmployeeRepository) {

@GetMapping("/employees")
fun getAllEmployees(): List<Employee> =
employeeRepository.findAll()

@PostMapping("/employees")
fun createNewEmployee(@RequestBody employee: Employee): Employee =
employeeRepository.save(employee)

@GetMapping("/employees/{id}")
fun getEmployeeById(@PathVariable(value = "id") employeeId: String):
ResponseEntity<Employee> {
return employeeRepository.findById(employeeId).map { emp ->
ResponseEntity.ok(emp)
}.orElse(ResponseEntity.notFound().build())
}

@PutMapping("/employees/{id}")
fun updateEmployeeById(@PathVariable(value = "id") employeeId: String,
@RequestBody newEmployee: Employee):
ResponseEntity<Employee> {

return employeeRepository.findById(employeeId).map { existingEmployee ->
val updatedEmployee: Employee = existingEmployee
.copy(firstName = newEmployee.firstName, lastName =
newEmployee.lastName, emailId = newEmployee.emailId)
ResponseEntity.ok().body(employeeRepository.save(updatedEmployee))
}.orElse(ResponseEntity.notFound().build())

}

@DeleteMapping("/employees/{id}")
fun deleteEmployeeById(@PathVariable(value = "id") employeeId: String):
ResponseEntity<Void> {
return employeeRepository.findById(employeeId).map { emp ->
employeeRepository.delete(emp)
ResponseEntity<Void>(HttpStatus.OK)
}.orElse(ResponseEntity.notFound().build())

}
}


Spring Boot Main Class:

Create the main class SpringbootKotlinCrudApplication.kt in the root package com.knf.dev.
package com.knf.dev

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

@SpringBootApplication
class SpringbootKotlinCrudApplication

fun main(args: Array<String>) {
runApplication<SpringbootKotlinCrudApplication>(*args)
}


Run:

Github repository download link is provided at the end of this tutorial

Local Setup:

Step 1: Download or clone the source code to a local machine.

Spring Boot 

Step 2mvn clean install

Step 3: Run the Spring Boot application
mvn spring-boot:run


Testing API's using Postman

Create an Employee:

Fetch All Employee:

Update Employee:

Delete Employee By Id:

Get Employee By Id:


More Kotlin practice:

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?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

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

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete