File tree Expand file tree Collapse file tree 6 files changed +126
-153
lines changed
springboot-webflux-2-restful/src/main/java/org/spring/springboot Expand file tree Collapse file tree 6 files changed +126
-153
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ package org .spring .springboot .dao ;
2
+
3
+ import org .spring .springboot .domain .City ;
4
+ import org .springframework .stereotype .Repository ;
5
+
6
+ import java .util .Collection ;
7
+ import java .util .concurrent .ConcurrentHashMap ;
8
+ import java .util .concurrent .ConcurrentMap ;
9
+ import java .util .concurrent .atomic .AtomicLong ;
10
+
11
+ @ Repository
12
+ public class CityRepository {
13
+
14
+ private ConcurrentMap <Long , City > repository = new ConcurrentHashMap <>();
15
+
16
+ private static final AtomicLong idGenerator = new AtomicLong (0 );
17
+
18
+ public Long save (City city ) {
19
+ Long id = idGenerator .incrementAndGet ();
20
+ city .setId (id );
21
+ repository .put (id , city );
22
+ return id ;
23
+ }
24
+
25
+ public Collection <City > findAll () {
26
+ return repository .values ();
27
+ }
28
+
29
+
30
+ public City findCityById (Long id ) {
31
+ return repository .get (id );
32
+ }
33
+
34
+ public Long updateCity (City city ) {
35
+ repository .put (city .getId (), city );
36
+ return city .getId ();
37
+ }
38
+
39
+ public Long deleteCity (Long id ) {
40
+ repository .remove (id );
41
+ return id ;
42
+ }
43
+ }
Original file line number Diff line number Diff line change
1
+ package org .spring .springboot .handler ;
2
+
3
+ import org .spring .springboot .dao .CityRepository ;
4
+ import org .spring .springboot .domain .City ;
5
+ import org .springframework .beans .factory .annotation .Autowired ;
6
+ import org .springframework .stereotype .Component ;
7
+ import reactor .core .publisher .Flux ;
8
+ import reactor .core .publisher .Mono ;
9
+
10
+ @ Component
11
+ public class CityHandler {
12
+
13
+ private final CityRepository cityRepository ;
14
+
15
+ @ Autowired
16
+ public CityHandler (CityRepository cityRepository ) {
17
+ this .cityRepository = cityRepository ;
18
+ }
19
+
20
+ public Mono <Long > save (City city ) {
21
+ return Mono .create (cityMonoSink -> cityMonoSink .success (cityRepository .save (city )));
22
+ }
23
+
24
+ public Mono <City > findCityById (Long id ) {
25
+ return Mono .create (cityMonoSink -> cityMonoSink .success (cityRepository .findCityById (id )));
26
+ }
27
+
28
+ public Flux <City > findAllCity () {
29
+ return Flux .create (cityFluxSink -> {
30
+ cityRepository .findAll ().forEach (city -> cityFluxSink .next (city ));
31
+ cityFluxSink .complete ();
32
+ });
33
+ }
34
+
35
+ public Mono <Long > modifyCity (City city ) {
36
+ return Mono .create (cityMonoSink -> cityMonoSink .success (cityRepository .updateCity (city )));
37
+ }
38
+
39
+ public Mono <Long > deleteCity (Long id ) {
40
+ return Mono .create (cityMonoSink -> cityMonoSink .success (cityRepository .deleteCity (id )));
41
+ }
42
+ }
Load Diff This file was deleted.
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
1
+ package org .spring .springboot .webflux .controller ;
2
+
3
+ import org .spring .springboot .domain .City ;
4
+ import org .spring .springboot .handler .CityHandler ;
5
+ import org .springframework .beans .factory .annotation .Autowired ;
6
+ import org .springframework .web .bind .annotation .*;
7
+ import reactor .core .publisher .Flux ;
8
+ import reactor .core .publisher .Mono ;
9
+
10
+ @ RestController
11
+ @ RequestMapping (value = "/city" )
12
+ public class CityWebFluxController {
13
+
14
+ @ Autowired
15
+ private CityHandler cityHandler ;
16
+
17
+ @ GetMapping (value = "/{id}" )
18
+ public Mono <City > findCityById (@ PathVariable ("id" ) Long id ) {
19
+ return cityHandler .findCityById (id );
20
+ }
21
+
22
+ @ GetMapping ()
23
+ public Flux <City > findAllCity () {
24
+ return cityHandler .findAllCity ();
25
+ }
26
+
27
+ @ PostMapping ()
28
+ public Mono <Long > saveCity (@ RequestBody City city ) {
29
+ return cityHandler .save (city );
30
+ }
31
+
32
+ @ PutMapping ()
33
+ public Mono <Long > modifyCity (@ RequestBody City city ) {
34
+ return cityHandler .modifyCity (city );
35
+ }
36
+
37
+ @ DeleteMapping (value = "/{id}" )
38
+ public Mono <Long > deleteCity (@ PathVariable ("id" ) Long id ) {
39
+ return cityHandler .deleteCity (id );
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments