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 POST, GET, PUT, DELETE
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.
CREATE TABLE "EMPLOYEE" ( "ID" NUMBER(10) NOT NULL ENABLE, "NAME" VARCHAR2(255 CHAR), "AGE" NUMBER(10), "DEPT" VARCHAR2(255 CHAR), PRIMARY KEY ("ID") );
Create packages
But before we go ahead create three more package in your project:
- co.tellmehow.kotlin (Main package file)
- co.tellmehow.kotlin.api (All api file would be here)
- co.tellmehow.kotlin.db (All database file would be here)
- co.tellmehow.kotlin.gs (All getter setter file would be here)
Employee.kt
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 @Entity data class Employee ( @Id 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 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
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 @RestController @RequestMapping("/employee") class RestController(val employeeRepository : EmployeeRepository) { @PostMapping fun saveEmployee(@RequestBody employee: Employee): Employee { return employeeRepository.save(employee) } @GetMapping("/{id}") fun getEmployee(@PathVariable id: Long): Employee { return employeeRepository.findOne(id) } @GetMapping fun getAllEmployees(): Iterable<Employee> { return employeeRepository.findAll() } @PutMapping fun updateEmployee(@RequestBody employee: Employee) { employeeRepository.save(employee) } @DeleteMapping("/{id}") fun deleteEmployee(@PathVariable id: Long) { employeeRepository.delete(id) } }
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 spring.datasource.url=jdbc:mysql://localhost:3306/mysql_database_name spring.datasource.username=mysql_username spring.datasource.password=mysql_password spring.datasource.driver-class-name=com.mysql.jdbc.Driver #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
[{"id":5,"name":"Sawan","age":26,"dept":"CSE"},{"id":6,"name":"Karn","age":25,"dept":"IT"}]
Now we’ll work on front end in next part.
Share your thoughts