-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerApplication.java
More file actions
136 lines (121 loc) · 5.37 KB
/
ServerApplication.java
File metadata and controls
136 lines (121 loc) · 5.37 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package b_webflux_helloworld.server;
import b_webflux_helloworld.shared.TextDto;
import io.reactivex.Flowable;
import io.reactivex.Single;
import io.reactivex.schedulers.Schedulers;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import static org.springframework.web.reactive.function.server.RequestPredicates.GET;
import static org.springframework.web.reactive.function.server.RequestPredicates.path;
import static org.springframework.web.reactive.function.server.RouterFunctions.nest;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.ok;
@SpringBootApplication(scanBasePackages = "b_webflux_helloworld.server")
public class ServerApplication {
public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);
}
@RestController
public static class HelloWorldReactorRestController {
@GetMapping(value = "/restcontroller/reactor/helloworld", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<String> helloworld() {
return Flux.concat(
Flux.just("H", "e", "l", "l", "o"),
Flux.just(" "),
Flux.just("W", "o", "r", "l", "d"),
Flux.just("!")
);
}
@GetMapping(value = "/restcontroller/reactor/helloworldslow", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flux<String> helloworldslow() {
return Flux.zip(
Flux.concat(
Flux.just("H", "e", "l", "l", "o"),
Flux.just(" "),
Flux.just("W", "o", "r", "l", "d"),
Flux.just("!")
),
Flux.interval(Duration.ofSeconds(1), reactor.core.scheduler.Schedulers.elastic()),
(s, ticker) -> s
);
}
@GetMapping(value = "/restcontroller/reactor/helloworlddto")
Mono<TextDto> helloworlddto() {
return Mono.just(new TextDto("Hello World!"));
}
}
@RestController
public static class HelloWorldRxJava2RestController {
@GetMapping(value = "/restcontroller/rxjava2/helloworld", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flowable<String> helloworld() {
return Flowable.concat(
Flowable.just("H", "e", "l", "l", "o"),
Flowable.just(" "),
Flowable.just("W", "o", "r", "l", "d"),
Flowable.just("!")
);
}
@GetMapping(value = "/restcontroller/rxjava2/helloworldslow", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
Flowable<String> helloworldslow() {
return Flowable.zip(
Flowable.concat(
Flowable.just("H", "e", "l", "l", "o"),
Flowable.just(" "),
Flowable.just("W", "o", "r", "l", "d"),
Flowable.just("!")
),
Flowable.interval(1, TimeUnit.SECONDS, Schedulers.io()),
(s, ticker) -> s
);
}
@GetMapping(value = "/restcontroller/rxjava2/helloworlddto")
Single<TextDto> helloworlddto() {
return Single.just(new TextDto("Hello World!"));
}
}
@Bean
RouterFunction<ServerResponse> routes() {
return nest(
path("/routerfunction"),
route(
GET("/helloworld"),
request -> ok()
.contentType(MediaType.TEXT_EVENT_STREAM)
.body(
helloWorldCharacterFlux(),
String.class
)
).andRoute(
GET("/helloworlddto"),
request -> ok()
.body(
Mono.just(new TextDto("Hello World!")),
TextDto.class
)
)
);
// hint: this looks much better using Kotlin. See counterRouter() in https://todd.ginsberg.com/post/springboot2-reactive-kotlin/
}
private Flux<String> helloWorldCharacterFlux() {
return Flux.zip(
Flux.concat(
Flux.just("H", "e", "l", "l", "o"),
Flux.just(" "),
Flux.just("W", "o", "r", "l", "d"),
Flux.just("!")
),
Flux.interval(Duration.ofSeconds(1), reactor.core.scheduler.Schedulers.elastic()),
(s, ticker) -> s
);
}
}