1
1
package com .example .demo ;
2
2
3
- import java .time .LocalDateTime ;
4
-
5
- import com .fasterxml .jackson .core .JsonProcessingException ;
6
- import com .fasterxml .jackson .databind .ObjectMapper ;
7
- import lombok .AllArgsConstructor ;
8
- import lombok .Builder ;
9
- import lombok .Data ;
10
- import lombok .NoArgsConstructor ;
11
- import lombok .ToString ;
3
+ import lombok .*;
12
4
import lombok .extern .slf4j .Slf4j ;
13
5
import org .springframework .boot .CommandLineRunner ;
14
6
import org .springframework .boot .SpringApplication ;
15
7
import org .springframework .boot .autoconfigure .SpringBootApplication ;
16
- import org .springframework .context .annotation .Bean ;
17
8
import org .springframework .core .annotation .Order ;
18
- import org .springframework .core .io .buffer .DataBuffer ;
19
- import org .springframework .core .io .buffer .DefaultDataBufferFactory ;
20
9
import org .springframework .data .annotation .CreatedDate ;
21
10
import org .springframework .data .annotation .Id ;
22
11
import org .springframework .data .mongodb .config .EnableMongoAuditing ;
23
12
import org .springframework .data .mongodb .core .mapping .Document ;
24
13
import org .springframework .data .mongodb .repository .ReactiveMongoRepository ;
25
- import org .springframework .http .HttpMethod ;
26
14
import org .springframework .http .HttpStatus ;
27
- import org .springframework .http .MediaType ;
28
- import org .springframework .security .authorization .AuthorizationDecision ;
29
- import org .springframework .security .config .annotation .web .reactive .EnableWebFluxSecurity ;
30
- import org .springframework .security .config .web .server .ServerHttpSecurity ;
31
- import org .springframework .security .core .Authentication ;
32
- import org .springframework .security .core .userdetails .MapReactiveUserDetailsService ;
33
- import org .springframework .security .core .userdetails .User ;
34
- import org .springframework .security .core .userdetails .UserDetails ;
35
- import org .springframework .security .web .server .SecurityWebFilterChain ;
36
- import org .springframework .security .web .server .authorization .AuthorizationContext ;
37
15
import org .springframework .stereotype .Component ;
38
- import org .springframework .web .bind .annotation .DeleteMapping ;
39
- import org .springframework .web .bind .annotation .GetMapping ;
40
- import org .springframework .web .bind .annotation .PathVariable ;
41
- import org .springframework .web .bind .annotation .PostMapping ;
42
- import org .springframework .web .bind .annotation .PutMapping ;
43
- import org .springframework .web .bind .annotation .RequestBody ;
44
- import org .springframework .web .bind .annotation .RequestMapping ;
45
- import org .springframework .web .bind .annotation .RestController ;
46
- import org .springframework .web .bind .support .WebExchangeBindException ;
16
+ import org .springframework .web .bind .annotation .*;
47
17
import org .springframework .web .server .ServerWebExchange ;
48
18
import org .springframework .web .server .WebExceptionHandler ;
49
19
import reactor .core .publisher .Flux ;
50
20
import reactor .core .publisher .Mono ;
51
21
22
+ import java .time .LocalDateTime ;
23
+
52
24
@ SpringBootApplication
53
25
@ EnableMongoAuditing
54
26
public class DemoApplication {
@@ -73,20 +45,20 @@ public DataInitializer(PostRepository posts) {
73
45
public void run (String [] args ) {
74
46
log .info ("start data initialization ..." );
75
47
this .posts
76
- .deleteAll ()
77
- .thenMany (
78
- Flux
79
- .just ("Post one" , "Post two" )
80
- .flatMap (
81
- title -> this .posts .save (Post .builder ().title (title ).content ("content of " + title ).build ())
82
- )
83
- )
84
- .log ()
85
- .subscribe (
86
- null ,
87
- null ,
88
- () -> log .info ("done initialization..." )
89
- );
48
+ .deleteAll ()
49
+ .thenMany (
50
+ Flux
51
+ .just ("Post one" , "Post two" )
52
+ .flatMap (
53
+ title -> this .posts .save (Post .builder ().title (title ).content ("content of " + title ).build ())
54
+ )
55
+ )
56
+ .log ()
57
+ .subscribe (
58
+ null ,
59
+ null ,
60
+ () -> log .info ("done initialization..." )
61
+ );
90
62
91
63
}
92
64
@@ -120,14 +92,14 @@ public Mono<Post> get(@PathVariable("id") String id) {
120
92
@ PutMapping ("/{id}" )
121
93
public Mono <Post > update (@ PathVariable ("id" ) String id , @ RequestBody Post post ) {
122
94
return this .posts .findById (id )
123
- .switchIfEmpty (Mono .error (new PostNotFoundException (id )))
124
- .map (p -> {
125
- p .setTitle (post .getTitle ());
126
- p .setContent (post .getContent ());
127
-
128
- return p ;
129
- })
130
- .flatMap (p -> this .posts .save (p ));
95
+ .switchIfEmpty (Mono .error (new PostNotFoundException (id )))
96
+ .map (p -> {
97
+ p .setTitle (post .getTitle ());
98
+ p .setContent (post .getContent ());
99
+
100
+ return p ;
101
+ })
102
+ .flatMap (p -> this .posts .save (p ));
131
103
}
132
104
133
105
@ DeleteMapping ("/{id}" )
@@ -139,7 +111,7 @@ public Mono<Void> delete(@PathVariable("id") String id) {
139
111
140
112
@ Component
141
113
@ Order (-2 )
142
- class RestWebExceptionHandler implements WebExceptionHandler {
114
+ class RestWebExceptionHandler implements WebExceptionHandler {
143
115
144
116
@ Override
145
117
public Mono <Void > handle (ServerWebExchange exchange , Throwable ex ) {
0 commit comments