Skip to content

Commit b0c3904

Browse files
author
Peter Vestlin
committed
Lade till lite klasser.
1 parent 9b5f008 commit b0c3904

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ dependencies {
3434
}
3535

3636
springBoot {
37-
mainClass = 'nu.westlin.kartrepo'
37+
mainClass = 'nu.westlin.kartrepo.ApplicationKt'
3838
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package nu.westlin.kartrepo
2+
3+
import org.springframework.boot.SpringApplication
4+
import org.springframework.boot.autoconfigure.SpringBootApplication
5+
6+
@SpringBootApplication
7+
open class Application
8+
9+
fun main(args: Array<String>) {
10+
SpringApplication.run(Application::class.java, *args)
11+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package nu.westlin.kartrepo
2+
3+
import org.springframework.http.HttpStatus
4+
import org.springframework.web.bind.annotation.*
5+
import javax.servlet.http.HttpServletResponse
6+
7+
@RestController
8+
class KartController {
9+
10+
@RequestMapping("/user")
11+
fun greeting(@RequestParam(value = "username", defaultValue = "pwestlin") username: String, response: HttpServletResponse): User {
12+
if (username == "pwestlin")
13+
return User(username, "Peter", "Westlin")
14+
else
15+
throw NotFoundException("User $username not found")
16+
17+
}
18+
19+
@RequestMapping("/users")
20+
fun greeting(): List<User> {
21+
return listOf(User("pwestlin", "Peter", "Westlin"))
22+
}
23+
24+
@ExceptionHandler(Exception::class)
25+
@ResponseBody
26+
@ResponseStatus(value = HttpStatus.NOT_FOUND)
27+
fun handleException(e: NotFoundException, response: HttpServletResponse): ErrorResource {
28+
return ErrorResource(HttpStatus.NOT_FOUND.value(), "Could not find resource")
29+
}
30+
}
31+
32+
class NotFoundException(message: String) : RuntimeException(message)
33+
34+
data class ErrorResource(val status: Int, val message: String)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package nu.westlin.kartrepo
2+
3+
data class User(val username: String, val firstname: String, val lastname: String)

0 commit comments

Comments
 (0)