KTLN-18: Kotlin Microservice With Spring Boot (#8913)
This commit is contained in:
parent
ebb53f0e5a
commit
73a571e395
|
@ -5,6 +5,7 @@
|
|||
<artifactId>spring-reactive-kotlin</artifactId>
|
||||
<name>spring-reactive-kotlin</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
|
@ -23,6 +24,18 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webflux</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot.experimental</groupId>
|
||||
<artifactId>spring-boot-starter-data-r2dbc</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.r2dbc</groupId>
|
||||
<artifactId>r2dbc-h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.module</groupId>
|
||||
<artifactId>jackson-module-kotlin</artifactId>
|
||||
|
@ -38,21 +51,95 @@
|
|||
<artifactId>reactor-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot.experimental</groupId>
|
||||
<artifactId>spring-boot-test-autoconfigure-r2dbc</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor</groupId>
|
||||
<artifactId>reactor-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot.experimental</groupId>
|
||||
<artifactId>spring-boot-bom-r2dbc</artifactId>
|
||||
<version>0.1.0.M3</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src/main/kotlin</sourceDirectory>
|
||||
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>kotlin-maven-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>compile</id>
|
||||
<phase>compile</phase>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>test-compile</id>
|
||||
<phase>test-compile</phase>
|
||||
<goals>
|
||||
<goal>test-compile</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<version>${kotlin.version}</version>
|
||||
<configuration>
|
||||
<args>
|
||||
<arg>-Xjsr305=strict</arg>
|
||||
</args>
|
||||
<jvmTarget>1.8</jvmTarget>
|
||||
<compilerPlugins>
|
||||
<plugin>spring</plugin>
|
||||
<plugin>jpa</plugin>
|
||||
</compilerPlugins>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-allopen</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jetbrains.kotlin</groupId>
|
||||
<artifactId>kotlin-maven-noarg</artifactId>
|
||||
<version>${kotlin.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<kotlin.version>1.3.70</kotlin.version>
|
||||
<boot.dependencies.version>2.2.5.RELEASE</boot.dependencies.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-milestones</id>
|
||||
<name>Spring Milestones</name>
|
||||
<url>https://repo.spring.io/milestone</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung.bootmicroservice
|
||||
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||
import org.springframework.boot.runApplication
|
||||
|
||||
@SpringBootApplication
|
||||
class HealthTrackerApplication
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
runApplication<HealthTrackerApplication>(*args)
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.bootmicroservice.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.data.r2dbc.core.DatabaseClient
|
||||
|
||||
@Configuration
|
||||
class DBConfiguration(db: DatabaseClient) {
|
||||
init {
|
||||
val initDb = db.execute {
|
||||
""" CREATE TABLE IF NOT EXISTS profile (
|
||||
id SERIAL PRIMARY KEY,
|
||||
first_name VARCHAR(20) NOT NULL,
|
||||
last_name VARCHAR(20) NOT NULL,
|
||||
birth_date DATE NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS health_record(
|
||||
id SERIAL PRIMARY KEY,
|
||||
profile_id LONG NOT NULL,
|
||||
temperature DECIMAL NOT NULL,
|
||||
blood_pressure DECIMAL NOT NULL,
|
||||
heart_rate DECIMAL,
|
||||
date DATE NOT NULL
|
||||
);
|
||||
"""
|
||||
}
|
||||
initDb.then().subscribe()
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
package com.baeldung.bootmicroservice.controller
|
||||
|
||||
import com.baeldung.bootmicroservice.model.AverageHealthStatus
|
||||
import com.baeldung.bootmicroservice.model.HealthRecord
|
||||
import com.baeldung.bootmicroservice.repository.HealthRecordRepository
|
||||
import org.springframework.web.bind.annotation.GetMapping
|
||||
import org.springframework.web.bind.annotation.PathVariable
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
@RestController
|
||||
class HealthRecordController(val repository: HealthRecordRepository) {
|
||||
|
||||
@PostMapping("/health/{profileId}/record")
|
||||
fun storeHealthRecord(@PathVariable("profileId") profileId: Long, @RequestBody record: HealthRecord): Mono<HealthRecord> =
|
||||
repository.save(HealthRecord(null
|
||||
, profileId
|
||||
, record.temperature
|
||||
, record.bloodPressure
|
||||
, record.heartRate
|
||||
, record.date))
|
||||
|
||||
@GetMapping("/health/{profileId}/avg")
|
||||
fun fetchHealthRecordAverage(@PathVariable("profileId") profileId: Long): Mono<AverageHealthStatus> =
|
||||
repository.findByProfileId(profileId)
|
||||
.reduce(
|
||||
AverageHealthStatus(0, 0.0, 0.0, 0.0)
|
||||
, { s, r ->
|
||||
AverageHealthStatus(s.cnt + 1
|
||||
, s.temperature + r.temperature
|
||||
, s.bloodPressure + r.bloodPressure
|
||||
, s.heartRate + r.heartRate
|
||||
)
|
||||
}
|
||||
).map { s ->
|
||||
AverageHealthStatus(s.cnt
|
||||
, if (s.cnt != 0) s.temperature / s.cnt else 0.0
|
||||
, if (s.cnt != 0) s.bloodPressure / s.cnt else 0.0
|
||||
, if (s.cnt != 0) s.heartRate / s.cnt else 0.0)
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package com.baeldung.bootmicroservice.controller
|
||||
|
||||
import com.baeldung.bootmicroservice.model.Profile
|
||||
import com.baeldung.bootmicroservice.repository.ProfileRepository
|
||||
import org.springframework.web.bind.annotation.PostMapping
|
||||
import org.springframework.web.bind.annotation.RequestBody
|
||||
import org.springframework.web.bind.annotation.RestController
|
||||
import reactor.core.publisher.Mono
|
||||
|
||||
@RestController
|
||||
class ProfileController(val repository: ProfileRepository) {
|
||||
|
||||
@PostMapping("/profile")
|
||||
fun save(@RequestBody profile: Profile): Mono<Profile> = repository.save(profile)
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
package com.baeldung.bootmicroservice.model;
|
||||
|
||||
class AverageHealthStatus(var cnt: Int, var temperature: Double, var bloodPressure: Double, var heartRate: Double)
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.bootmicroservice.model
|
||||
|
||||
import org.springframework.data.annotation.Id
|
||||
import org.springframework.data.relational.core.mapping.Table
|
||||
import java.time.LocalDate
|
||||
|
||||
@Table
|
||||
data class HealthRecord(@Id var id: Long?, var profileId: Long?, var temperature: Double, var bloodPressure: Double, var heartRate: Double, var date: LocalDate)
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.bootmicroservice.model
|
||||
|
||||
import org.springframework.data.annotation.Id
|
||||
import org.springframework.data.relational.core.mapping.Table
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@Table
|
||||
data class Profile(@Id var id:Long?, var firstName : String, var lastName : String, var birthDate: LocalDateTime)
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.bootmicroservice.repository
|
||||
|
||||
import com.baeldung.bootmicroservice.model.HealthRecord
|
||||
import org.springframework.data.r2dbc.repository.Query
|
||||
import org.springframework.data.repository.reactive.ReactiveCrudRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
import reactor.core.publisher.Flux
|
||||
|
||||
@Repository
|
||||
interface HealthRecordRepository: ReactiveCrudRepository<HealthRecord, Long> {
|
||||
@Query("select p.* from health_record p where p.profile_id = :profileId ")
|
||||
fun findByProfileId(profileId: Long): Flux<HealthRecord>
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
package com.baeldung.bootmicroservice.repository
|
||||
|
||||
import com.baeldung.bootmicroservice.model.Profile
|
||||
import org.springframework.data.repository.reactive.ReactiveCrudRepository
|
||||
import org.springframework.stereotype.Repository
|
||||
|
||||
@Repository
|
||||
interface ProfileRepository: ReactiveCrudRepository<Profile, Long>
|
|
@ -0,0 +1 @@
|
|||
management.endpoints.web.exposure.include: health,metrics
|
|
@ -0,0 +1,51 @@
|
|||
package com.baeldung.bootmicroservice.controller;
|
||||
|
||||
import com.baeldung.bootmicroservice.model.Profile
|
||||
import com.fasterxml.jackson.databind.ObjectMapper
|
||||
import org.junit.jupiter.api.BeforeEach
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.springframework.beans.factory.annotation.Autowired
|
||||
import org.springframework.boot.test.context.SpringBootTest
|
||||
import org.springframework.http.MediaType
|
||||
import org.springframework.test.web.reactive.server.WebTestClient
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@SpringBootTest
|
||||
class ProfileControllerTest {
|
||||
@Autowired
|
||||
lateinit var controller: ProfileController
|
||||
|
||||
@Autowired
|
||||
lateinit var mapper: ObjectMapper ;
|
||||
|
||||
lateinit var client: WebTestClient
|
||||
lateinit var profile: String
|
||||
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
client = WebTestClient.bindToController(controller).build()
|
||||
profile = mapper.writeValueAsString(Profile(null, "kotlin", "reactive", LocalDateTime.now()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenRequestProfile_thenStatusShouldBeOk() {
|
||||
client.post()
|
||||
.uri("/profile")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(profile)
|
||||
.exchange()
|
||||
.expectStatus().isOk
|
||||
}
|
||||
|
||||
@Test
|
||||
fun whenRequestProfile_thenIdShouldBeNotNull() {
|
||||
client.post()
|
||||
.uri("/profile")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.bodyValue(profile)
|
||||
.exchange()
|
||||
.expectBody()
|
||||
.jsonPath("$.id")
|
||||
.isNotEmpty
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue