/
UserController.kt
78 lines (67 loc) · 2.6 KB
/
UserController.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package spring.kotlin.web.controller
import org.springframework.http.MediaType.APPLICATION_JSON
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.fromPublisher
import org.springframework.web.reactive.function.server.HandlerFunction
import org.springframework.web.reactive.function.server.RequestPredicates.contentType
import org.springframework.web.reactive.function.server.RouterFunction
import org.springframework.web.reactive.function.server.ServerRequest
import org.springframework.web.reactive.function.server.ServerResponse
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import sample.PersonHandler
import spring.kotlin.web.entity.User
import spring.kotlin.web.util.invoke
//@Component
class UserController : RouterFunction<ServerResponse> {
val personHandler = PersonHandler()
override fun route(request: ServerRequest) =
"/user" {
GET(findAll())
"/empty" {
GET { req ->
ServerResponse.ok().body(fromPublisher(Flux.just(User("empty"))))
}
}
"/login/{login}"{
GET(::findOther)
}
"/controller"{
GET(this@UserController::findOne)
}
"/comp"{
GET(Companion::findOtherOne)
}
"/java"{
GET(personHandler::getPerson)
}
"/javaStatic"{
GET(PersonHandler::getOtherPerson)
}
(GET("/include") and contentType(APPLICATION_JSON)) {
include()
}
"/**"{
(!GET("/exclude")) {
findAll()
}
}
}(request)
fun findOne(req: ServerRequest): Mono<ServerResponse> {
return ServerResponse.ok().body(fromPublisher(Mono.just(User("findOne"))))
}
fun include() = HandlerFunction {
ServerResponse.ok().body(fromPublisher(Flux.just(User("include"))))
}
fun findAll() = HandlerFunction {
ServerResponse.ok().body(fromPublisher(Flux.just(User("first"), User("second"))))
}
companion object {
fun findOtherOne(req: ServerRequest): Mono<ServerResponse> {
return ServerResponse.ok().body(fromPublisher(Flux.just(User("findOtherOne"))))
}
}
}
fun findOther(req: ServerRequest): Mono<ServerResponse> {
return ServerResponse.ok().body(fromPublisher(Flux.just(User("findOther"))))
}