Skip to content
This repository was archived by the owner on Sep 16, 2019. It is now read-only.

Commit cffc9d1

Browse files
committed
Add simple service with a GET and a POST request
1 parent 3f18955 commit cffc9d1

File tree

11 files changed

+186
-1
lines changed

11 files changed

+186
-1
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
############
44

55
.gradle
6-
/build/
6+
build/
77

88
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
99
!gradle-wrapper.jar

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Kotlin Microservices Spring Boot Example
2+
3+
Services
4+
* [bankofhamburg-customer-service](bankofhamburg-customer-service) Example service with a GET and POST endpoint
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Bank of Hamburg Customer Service
2+
3+
Run service
4+
```
5+
../gradlew bootRun
6+
```
7+
8+
### Exemplary usage
9+
10+
`helloWorld` endpoint: GET request
11+
```
12+
curl -v http://localhost:8080/helloWorld?type=fuu
13+
```
14+
15+
`customer` endpoint: POST request
16+
```
17+
curl -v -F firstName=Jana -F lastName=Müller http://localhost:8080/customer
18+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
val jar by tasks.getting(Jar::class) {
2+
baseName = "bankofhamburg-customer-service"
3+
version = "0.1.0"
4+
}
5+
6+
application {
7+
mainClassName = "org.example.bankofhamburg.customer.ApplicationKt" // Required for Gradle Kotlin DSL
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package org.example.bankofhamburg.customer
2+
3+
import org.springframework.boot.SpringApplication
4+
import org.springframework.boot.autoconfigure.SpringBootApplication
5+
6+
7+
@SpringBootApplication
8+
class Application
9+
10+
fun main(args: Array<String>) {
11+
SpringApplication.run(Application::class.java, *args)
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.example.bankofhamburg.customer
2+
3+
import org.springframework.http.HttpStatus
4+
import org.springframework.http.MediaType
5+
import org.springframework.http.ResponseEntity
6+
import org.springframework.web.bind.annotation.PostMapping
7+
import org.springframework.web.bind.annotation.RequestParam
8+
import org.springframework.web.bind.annotation.RestController
9+
10+
11+
@RestController
12+
class CustomerController {
13+
14+
@PostMapping("/customer", consumes = [MediaType.MULTIPART_FORM_DATA_VALUE])
15+
fun customer(@RequestParam("firstName") firstName: String, @RequestParam("lastName") lastName: String): ResponseEntity<String> {
16+
println("Hello $firstName $lastName")
17+
return ResponseEntity(HttpStatus.CREATED)
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.example.bankofhamburg.customer
2+
3+
import org.springframework.http.HttpStatus
4+
import org.springframework.http.ResponseEntity
5+
import org.springframework.web.bind.annotation.GetMapping
6+
import org.springframework.web.bind.annotation.RequestParam
7+
import org.springframework.web.bind.annotation.RestController
8+
9+
10+
@RestController
11+
class HelloWorldController {
12+
13+
@GetMapping("/helloWorld")
14+
fun helloWorld(@RequestParam(value = "type", defaultValue = "pong") type: String): ResponseEntity<String> {
15+
return ResponseEntity("Type is $type", HttpStatus.OK)
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.example.bankofhamburg.customer
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
8+
import org.springframework.beans.factory.annotation.Autowired
9+
import org.springframework.boot.test.context.SpringBootTest
10+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
11+
import org.springframework.test.context.junit4.SpringRunner
12+
import org.springframework.boot.test.web.client.TestRestTemplate
13+
import org.springframework.util.LinkedMultiValueMap
14+
import org.springframework.http.HttpEntity
15+
import org.springframework.http.HttpHeaders
16+
import org.springframework.http.HttpStatus
17+
import org.springframework.http.MediaType
18+
19+
20+
@RunWith(SpringRunner::class)
21+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
22+
class CustomerControllerTest {
23+
24+
@Autowired
25+
lateinit var restTemplate: TestRestTemplate
26+
27+
@Test
28+
fun customerTest() {
29+
val requestEntity = createEntity("Jana", "Müller")
30+
val responseEntity = this.restTemplate.postForEntity("/customer", requestEntity, String::class.java)
31+
assertThat(responseEntity.statusCodeValue).isEqualTo(HttpStatus.CREATED.value())
32+
}
33+
34+
private fun createEntity(firstName: String, lastName: String): HttpEntity<LinkedMultiValueMap<String, String>> {
35+
val body = LinkedMultiValueMap<String, String>()
36+
body.add("firstName", firstName)
37+
body.add("lastName", lastName)
38+
39+
val headers = HttpHeaders()
40+
headers.contentType = MediaType.MULTIPART_FORM_DATA
41+
return HttpEntity(body, headers)
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.example.bankofhamburg.customer
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
5+
import org.junit.Test
6+
import org.junit.runner.RunWith
7+
8+
import org.springframework.beans.factory.annotation.Autowired
9+
import org.springframework.boot.test.context.SpringBootTest
10+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment
11+
import org.springframework.test.context.junit4.SpringRunner
12+
import org.springframework.boot.test.web.client.TestRestTemplate
13+
import org.springframework.http.HttpStatus
14+
15+
16+
@RunWith(SpringRunner::class)
17+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
18+
class HelloWorldControllerTest {
19+
20+
@Autowired
21+
lateinit var restTemplate: TestRestTemplate
22+
23+
@Test
24+
fun helloWorldTest() {
25+
val responseEntity = this.restTemplate.getForEntity("/helloWorld?type=fuu", String::class.java)
26+
assertThat(responseEntity.statusCodeValue).isEqualTo(HttpStatus.OK.value())
27+
assertThat(responseEntity.body).isEqualTo("Type is fuu")
28+
}
29+
30+
}

build.gradle.kts

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
plugins {
2+
val kotlinVersion = "1.2.41"
3+
4+
application // Required for Gradle Kotlin DSL
5+
6+
// Plugins used by submodules only
7+
kotlin("jvm") version kotlinVersion apply false
8+
kotlin("plugin.spring") version kotlinVersion apply false
9+
id("org.springframework.boot") version "1.5.4.RELEASE" apply false
10+
}
11+
12+
// Configure Kotlin and Spring Boot for all Projects
13+
subprojects {
14+
15+
apply {
16+
plugin("application") // Required for Gradle Kotlin DSL
17+
plugin("kotlin")
18+
plugin("kotlin-spring")
19+
plugin("org.springframework.boot")
20+
}
21+
22+
repositories {
23+
jcenter()
24+
}
25+
26+
dependencies {
27+
compile(kotlin("stdlib"))
28+
compile("org.springframework.boot:spring-boot-starter-web")
29+
testCompile("org.springframework.boot:spring-boot-starter-test")
30+
}
31+
}

settings.gradle.kts

+3
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
rootProject.name = "bankofhamburg-services"
2+
3+
include(
4+
"bankofhamburg-customer-service")

0 commit comments

Comments
 (0)