File tree Expand file tree Collapse file tree
spring-5-reactive-client/src/test/java/com/baeldung/reactive
spring-5-reactive/src/main/java/com/baeldung/reactive/controller Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .reactive ;
2+
3+ import org .springframework .boot .CommandLineRunner ;
4+ import org .springframework .boot .SpringApplication ;
5+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
6+ import org .springframework .http .HttpHeaders ;
7+ import org .springframework .http .MediaType ;
8+ import org .springframework .web .reactive .function .client .WebClient ;
9+ import reactor .core .publisher .Flux ;
10+
11+ @ SpringBootApplication
12+ public class ReactiveEventClient implements CommandLineRunner {
13+ public static void main (String [] args ) {
14+ System .setProperty ("server.port" ,"8090" );
15+ SpringApplication .run (ReactiveEventClient .class , args );
16+ }
17+
18+ @ Override
19+ public void run (String ... args ) throws Exception {
20+ WebClient webclient = WebClient .builder ()
21+ .defaultHeader (HttpHeaders .ACCEPT , MediaType .TEXT_EVENT_STREAM_VALUE )
22+ .baseUrl ("http://localhost:8080/events" )
23+ .build ();
24+
25+ webclient .get ().uri ("/stream/" ).header (HttpHeaders .CONTENT_TYPE ,MediaType .TEXT_HTML .toString ())
26+ .retrieve ().bodyToFlux (String .class )
27+ .subscribe (System .out ::println );
28+ }
29+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .reactive .controller ;
2+
3+
4+ import org .springframework .http .MediaType ;
5+ import org .springframework .web .bind .annotation .GetMapping ;
6+ import org .springframework .web .bind .annotation .RequestMapping ;
7+ import org .springframework .web .bind .annotation .RestController ;
8+ import reactor .core .publisher .Flux ;
9+
10+ import java .time .Duration ;
11+
12+
13+ @ RestController
14+ @ RequestMapping ("/events" )
15+ public class ReactiveEventServer {
16+
17+ @ GetMapping (path = "/stream/" , produces =MediaType .TEXT_EVENT_STREAM_VALUE )
18+ public Flux <String > eventStream () {
19+ return Flux .interval (Duration .ofSeconds (1 )).map (tick -> {return "Event:" + tick ;});
20+ }
21+ }
You can’t perform that action at this time.
0 commit comments