File tree 9 files changed +143
-1
lines changed
java/spring/znevzz/reactive
9 files changed +143
-1
lines changed Original file line number Diff line number Diff line change
1
+ package spring .znevzz .reactive .bean ;
2
+
3
+ import reactor .core .publisher .Mono ;
4
+
5
+ public interface ICacheRequest <T > {
6
+ String getUser ();
7
+ T getPayload ();
8
+ }
Original file line number Diff line number Diff line change
1
+ package spring .znevzz .reactive .bean ;
2
+
3
+ import java .time .LocalDate ;
4
+ import java .util .Optional ;
5
+
6
+ public interface ICacheResponse <R > {
7
+ LocalDate getLastUpdated ();
8
+ Optional <R > getPayload ();
9
+ }
Original file line number Diff line number Diff line change
1
+ package spring .znevzz .reactive .bean ;
2
+
3
+ import lombok .Data ;
4
+
5
+ @ Data
6
+ public class SimpleCacheRequest implements ICacheRequest <String > {
7
+
8
+ private String user ;
9
+ private String payload ;
10
+
11
+ @ Override
12
+ public String getUser () {
13
+ return "" ;
14
+ }
15
+
16
+ @ Override
17
+ public String getPayload () {
18
+ return this .payload ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package spring .znevzz .reactive .bean ;
2
+
3
+ import lombok .Data ;
4
+
5
+ import java .time .LocalDate ;
6
+ import java .util .Optional ;
7
+
8
+ @ Data
9
+ public class SimpleCacheResponse implements ICacheResponse <String > {
10
+
11
+ private String payload ;
12
+
13
+ @ Override
14
+ public LocalDate getLastUpdated () {
15
+ return LocalDate .now ();
16
+ }
17
+
18
+ @ Override
19
+ public Optional <String > getPayload () {
20
+ return Optional .ofNullable (payload );
21
+ }
22
+
23
+
24
+ }
Original file line number Diff line number Diff line change
1
+ package spring .znevzz .reactive .handler ;
2
+
3
+ import org .springframework .stereotype .Component ;
4
+ import org .springframework .web .reactive .function .server .ServerRequest ;
5
+ import reactor .core .publisher .Flux ;
6
+ import reactor .core .publisher .Mono ;
7
+ import spring .znevzz .reactive .bean .ICacheRequest ;
8
+ import spring .znevzz .reactive .bean .ICacheResponse ;
9
+
10
+ @ Component
11
+ public class CacheRequestHandler implements IRequestHandler <ICacheRequest , ICacheResponse > {
12
+
13
+ @ Override
14
+ public Flux <ICacheResponse > get (ICacheRequest iCacheRequest ) {
15
+ return Flux .empty ();
16
+ }
17
+
18
+ @ Override
19
+ public Mono handle (ServerRequest serverRequest ) {
20
+ return Mono .empty ();
21
+ }
22
+ }
Original file line number Diff line number Diff line change
1
+ package spring .znevzz .reactive .handler ;
2
+
3
+ import org .springframework .http .server .reactive .ServerHttpResponse ;
4
+ import org .springframework .http .server .reactive .ServerHttpResponseDecorator ;
5
+ import org .springframework .web .reactive .function .server .HandlerFunction ;
6
+ import org .springframework .web .reactive .function .server .ServerRequest ;
7
+ import org .springframework .web .reactive .function .server .ServerResponse ;
8
+ import reactor .core .publisher .Flux ;
9
+ import reactor .core .publisher .Mono ;
10
+
11
+ import static org .springframework .web .reactive .function .server .ServerResponse .ok ;
12
+
13
+ public interface IRequestHandler <T ,R > extends HandlerFunction {
14
+
15
+ Flux <R > get (T t );
16
+
17
+ default <T extends ServerResponse > Mono <T > welcome (ServerRequest serverRequest ) {
18
+ Mono welcomeMessage = Mono .empty ();
19
+ Mono <String > responseMessage = Mono .just ("welcome" );
20
+ welcomeMessage = ok ()
21
+ .hint ("key" , "value" )
22
+ .body (responseMessage , String .class )
23
+ ;
24
+ return welcomeMessage ;
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ package spring .znevzz .reactive .router ;
2
+
3
+
4
+ import org .springframework .context .annotation .Bean ;
5
+ import org .springframework .context .annotation .Configuration ;
6
+ import org .springframework .web .reactive .function .server .RouterFunction ;
7
+ import org .springframework .web .reactive .function .server .RouterFunctions ;
8
+ import spring .znevzz .reactive .handler .IRequestHandler ;
9
+
10
+ import static org .springframework .http .MediaType .APPLICATION_JSON ;
11
+ import static org .springframework .web .reactive .function .server .RequestPredicates .GET ;
12
+ import static org .springframework .web .reactive .function .server .RequestPredicates .accept ;
13
+
14
+ /**
15
+ * more information at:
16
+ * - https://spring.io/blog/2016/09/22/new-in-spring-5-functional-web-framework
17
+ * - https://dzone.com/articles/creating-multiple-routerfunctions-in-spring-webflux
18
+ */
19
+
20
+ @ Configuration
21
+ public class CacheRouter {
22
+ @ Bean
23
+ public RouterFunction routers (IRequestHandler handler ) {
24
+ return RouterFunctions .route (
25
+ GET ("/" )
26
+ .and (accept (APPLICATION_JSON )),
27
+ handler ::welcome );
28
+ }
29
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ spring :
2
+ application :
3
+ name : spring-caching-app
4
+ server :
5
+ port : 10001
You can’t perform that action at this time.
0 commit comments