Spring Boot - Kotlin

Overview

A spring boot project with Kotlin

Github: https://github.com/gitorko/project03

Kotlin

Kotlin is a cross-platform, statically typed, general-purpose high-level programming language with type inference. Kotlin is designed to interoperate fully with Java, and the JVM version of Kotlin's standard library depends on the Java Class Library, but type inference allows its syntax to be more concise.

Code

 1package com.demo.project03
 2
 3import com.demo.project03.domain.Customer
 4import com.demo.project03.repo.CustomerRepository
 5import org.slf4j.Logger
 6import org.slf4j.LoggerFactory
 7import org.springframework.boot.CommandLineRunner
 8import org.springframework.boot.autoconfigure.SpringBootApplication
 9import org.springframework.boot.runApplication
10import org.springframework.context.annotation.Bean
11
12@SpringBootApplication
13class Main {
14    private val log: Logger = LoggerFactory.getLogger(this::class.java)
15
16    @Bean
17    fun onStart(repo: CustomerRepository) = CommandLineRunner {
18        log.info("Seeding!")
19        val customer = Customer(0, "Jack", 35)
20        repo.save(customer)
21    }
22}
23
24fun main(args: Array<String>) {
25    runApplication<Main>(*args)
26}
 1package com.demo.project03.controller
 2
 3import com.demo.project03.domain.Customer
 4import com.demo.project03.repo.CustomerRepository
 5import org.slf4j.Logger
 6import org.slf4j.LoggerFactory
 7import org.springframework.beans.factory.annotation.Autowired
 8import org.springframework.web.bind.annotation.GetMapping
 9import org.springframework.web.bind.annotation.PostMapping
10import org.springframework.web.bind.annotation.RequestBody
11import org.springframework.web.bind.annotation.RestController
12
13@RestController
14class HomeController {
15
16    private val log: Logger = LoggerFactory.getLogger(this::class.java)
17
18    @Autowired
19    lateinit var repo: CustomerRepository
20
21    @GetMapping("/customer")
22    fun getCustomer(): MutableIterable<Customer> {
23        log.info("Getting customers!")
24        return repo.findAll()
25    }
26
27    @PostMapping("/customer")
28    fun save(@RequestBody customer: Customer): Customer {
29        return repo.save(customer)
30    }
31}
 1package com.demo.project03.domain
 2
 3import jakarta.persistence.*
 4import java.time.LocalDateTime
 5
 6@Entity
 7@Table(name = "customer")
 8data class Customer(
 9        @Id
10        @GeneratedValue(strategy = GenerationType.IDENTITY)
11        val id: Long = 0,
12        val name: String,
13        val age: Int,
14        val createdAt: LocalDateTime = LocalDateTime.now(),
15)
1package com.demo.project03.repo
2
3import com.demo.project03.domain.Customer
4import org.springframework.data.jpa.repository.JpaRepository
5import org.springframework.stereotype.Repository
6
7@Repository
8interface CustomerRepository : JpaRepository<Customer, Long>

Postman

Import the postman collection to postman

Postman Collection

Setup

 1# Project03
 2
 3Kotlin Spring Boot Rest
 4
 5[https://github.com/gitorko/project03](https://github.com/gitorko/project03)
 6
 7### Version
 8
 9Check version
10
11```bash
12$java --version
13openjdk version "21.0.3" 2024-04-16 LTS
14```
15
16### Postgres DB
17
18```
19docker run -p 5432:5432 --name pg-container -e POSTGRES_PASSWORD=password -d postgres:14
20docker ps
21docker exec -it pg-container psql -U postgres -W postgres
22CREATE USER test WITH PASSWORD 'test@123';
23CREATE DATABASE "test-db" WITH OWNER "test" ENCODING UTF8 TEMPLATE template0;
24grant all PRIVILEGES ON DATABASE "test-db" to test;
25
26docker stop pg-container
27docker start pg-container
28```
29
30### Dev
31
32To run the backend in dev mode.
33
34```bash
35./gradlew clean build
36./gradlew bootRun
37```
38
39### Prod
40
41To run as a single jar, both UI and backend are bundled to single uber jar.
42
43```bash
44./gradlew bootJar
45java -jar build/libs/project03-1.0.0.jar
46```

References

https://kotlinlang.org/docs/jvm-spring-boot-restful.html

comments powered by Disqus