Skip to content

Commit 9b7fa94

Browse files
committed
finished tutorial
1 parent 96ca35c commit 9b7fa94

File tree

6 files changed

+133
-2
lines changed

6 files changed

+133
-2
lines changed

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,17 @@
11
Simple REST API Kotlin + Spring Boot
22
====================================
33

4-
Code from following tutorial - https://scotch.io/@grahamcox82/how-to-build-a-simple-rest-api-with-kotlin-and-spring-boot
4+
Code from this [scotch.io tutorial](https://scotch.io/@grahamcox82/how-to-build-a-simple-rest-api-with-kotlin-and-spring-boot).
5+
6+
[Tutorial Code](https://github.com/sazzer/kotlin-with-spring-boot)
7+
8+
---
9+
10+
This repository is an example on how to write a Web API using the Kotlin programming language and the Spring Boot framework.
11+
12+
Build and run the example by simply executing `gradle bootRun`, and then call the various endpoints to see the examples in action:
13+
14+
* http://localhost:8080/answer
15+
* http://localhost:8080/user
16+
* http://localhost:8080/string/abc[?operation=[reverse,upper,lower]]
17+
* http://localhost:8080/raiseError

src/main/kotlin/tutorial/Application.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tutorial
22

33
import org.springframework.boot.SpringApplication
44
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
5+
import org.springframework.context.annotation.Bean
56
import org.springframework.context.annotation.Configuration
67

78

@@ -12,7 +13,11 @@ import org.springframework.context.annotation.Configuration
1213
*/
1314
@EnableAutoConfiguration
1415
@Configuration
15-
class Application
16+
class Application {
17+
18+
@Bean
19+
fun controller() = FirstController()
20+
}
1621

1722
/**
1823
* Run the application
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package tutorial
2+
3+
import org.springframework.http.HttpStatus
4+
import org.springframework.web.bind.annotation.ExceptionHandler
5+
import org.springframework.web.bind.annotation.PathVariable
6+
import org.springframework.web.bind.annotation.RequestBody
7+
import org.springframework.web.bind.annotation.RequestMapping
8+
import org.springframework.web.bind.annotation.RequestMethod
9+
import org.springframework.web.bind.annotation.RequestParam
10+
import org.springframework.web.bind.annotation.ResponseStatus
11+
import org.springframework.web.bind.annotation.RestController
12+
import java.time.Instant
13+
14+
@RestController
15+
class FirstController {
16+
17+
/**
18+
* The ultimate answer to life, the universe and everything
19+
*/
20+
@RequestMapping("/answer")
21+
fun answer() = 42
22+
23+
/**
24+
* Get the details of a user
25+
*/
26+
@RequestMapping("/user")
27+
fun user(): User = User(
28+
username = "nrojiani",
29+
screenName = "Navid",
30+
email = "[email protected]",
31+
registered = Instant.now()
32+
)
33+
34+
/**
35+
* Perform some string manipulation on the given value
36+
* @param value The value to manipulate (path variable)
37+
* @param operation The operation to perform (query param)
38+
*/
39+
@RequestMapping("/string/{value}")
40+
@ResponseStatus(HttpStatus.CREATED)
41+
fun manipulateString(
42+
@PathVariable value: String,
43+
@RequestParam(name = "operation", defaultValue = "none") operation: String
44+
): String = when (operation.toUpperCase()) {
45+
"REVERSE" -> value.reversed()
46+
"UPPER" -> value.toUpperCase()
47+
"LOWER" -> value.toLowerCase()
48+
else -> value
49+
}
50+
51+
/**
52+
* Pretend to create a new user
53+
* @param user The details of the user to create
54+
*/
55+
@RequestMapping(value = "/user", method = arrayOf(RequestMethod.POST))
56+
fun createUser(@RequestBody user: NewUser): User {
57+
return User(
58+
username = user.username,
59+
screenName = user.screenName,
60+
email = user.email,
61+
registered = Instant.now()
62+
)
63+
}
64+
65+
/** Cause an error to occur */
66+
@RequestMapping("/raiseError")
67+
fun raiseError(): Unit = throw IllegalArgumentException("This shouldn't have happened")
68+
69+
/** Handle the error */
70+
@ExceptionHandler(IllegalArgumentException::class)
71+
@ResponseStatus(code = HttpStatus.CONFLICT)
72+
fun handleError(e: IllegalArgumentException): String? = e.message
73+
74+
}

src/main/kotlin/tutorial/NewUser.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package tutorial
2+
3+
import com.fasterxml.jackson.annotation.JsonCreator
4+
5+
/**
6+
* Representation of a User to create
7+
* @property username The username of the user
8+
* @property screenName The screen name of the user
9+
* @property email The email address of the user
10+
*/
11+
data class NewUser
12+
@JsonCreator constructor(
13+
val username: String,
14+
val screenName: String,
15+
val email: String
16+
)

src/main/kotlin/tutorial/User.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package tutorial
2+
3+
import java.time.Instant
4+
5+
/**
6+
* Representation of a User
7+
* @property username The username of the user
8+
* @property screenName The screen name of the user
9+
* @property email The email address of the user
10+
* @property registered When the user registered with us
11+
*/
12+
data class User(
13+
val username: String,
14+
val screenName: String,
15+
val email: String,
16+
val registered: Instant
17+
)

src/main/resources/application.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
spring:
2+
jackson:
3+
serialization:
4+
indent_output: true
5+
write_dates_as_timestamps: false
6+
write_durations_as_timestamps: false

0 commit comments

Comments
 (0)