Spring Boot + Kotlin + AngularJS + WebJar CRUD Example Part 3

Spring Boot + Kotlin + AngularJS + WebJar CRUD Example
Spring Boot + Kotlin + AngularJS + WebJar CRUD Example

In this tutorial we previously discuss about Spring Boot and Kotlin and now we’ll create REST service using Spring Boot with Kotlin. Our application offers all four CRUD operations using the respective HTTP verbs POSTGETPUTDELETE

Based on the above HTTP verbs, our REST API does the below

  • /employee      –> Create Employee(POST)
  • /employee/1  –> Get Employee By Id (GET)
  • /employee      –> List of All Employees (GET)
  • /employee  –> Update Employee (PUT)
  • /employee/1  –> Delete Employee (DELETE)

Creating table

Create EMPLOYEE Table, simply Copy and Paste the following SQL query in the query editor to get the table created.

    "NAME" VARCHAR2(255 CHAR), 
    "AGE" NUMBER(10), 
    "DEPT" VARCHAR2(255 CHAR),   

Create packages

But before we go ahead create three more package in your project:

  1. co.tellmehow.kotlin (Main package file)
  2. co.tellmehow.kotlin.api (All api file would be here)
  3. co.tellmehow.kotlin.db (All database file would be here)
  4. co.tellmehow.kotlin.gs (All getter setter file would be here)


Create Employee.kt in package co.tellmehow.kotlin.gs. We will be receiving the response of Object Payload in the form of JSON rather than primitive values. REST uses JSON for both making requests and sending responses. So let’s create a data class to represent an object

import javax.persistence.Entity
import javax.persistence.Id

data class Employee (
  var id : Long=0,
  var name : String="",
  var age : Int=0,
  var dept : String=""

Employee.kt acts as our data class with the id, name, age, dept property

Add some dependencies in pom.xml

When you create Employee.kt then you’ll get some error in this kotlin class because you’ve to add some more dependencies in pom.xml.

<!-- Spring boot rest -->

EmployeeRepository interface

Create EmployeeRepository interface in o.tellmehow.kotlin.db in which CrudRepository which is interface for generic CRUD operations on a repository for a specific type. We will be able to perform CRUD Operations using the built-in methods of CrudRepository.

import org.springframework.data.repository.CrudRepository
import co.tellmehow.kotlin.gs.Employee

interface EmployeeRepository : CrudRepository<Employee, Long>{}

Working with API

Create RestController.kt in co.tellmehow.kotlin.api and our REST Endpoints looks like below

import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PathVariable
import org.springframework.web.bind.annotation.PutMapping
import org.springframework.web.bind.annotation.DeleteMapping
import co.tellmehow.kotlin.gs.Employee
import co.tellmehow.kotlin.db.EmployeeRepository

class RestController(val employeeRepository : EmployeeRepository) {
  fun saveEmployee(@RequestBody employee: Employee): Employee
    return employeeRepository.save(employee)

  fun getEmployee(@PathVariable id: Long): Employee
    return employeeRepository.findOne(id)

  fun getAllEmployees(): Iterable<Employee>
    return employeeRepository.findAll()

  fun updateEmployee(@RequestBody employee: Employee)

  fun deleteEmployee(@PathVariable id: Long)

Add datasource in properties file

If you run this application you’ll get error like : “Cannot determine embedded database driver class for database type NONE” because we forgot to add datasource in application.properties file.

#MySQL Connection settings

#JPA properties
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update

Now you can run your and check output: http://localhost:8080/employee


Now we’ll work on front end in next part.

Share your thoughts

Loading Facebook Comments ...
Loading Disqus Comments ...