Skip to content

Commit 63d50b4

Browse files
committed
Polishing before the big release.
1 parent 6191a5c commit 63d50b4

File tree

26 files changed

+104
-145
lines changed

26 files changed

+104
-145
lines changed

1-reactive/src/main/java/com/greglturnquist/hackingspringboot/reactive/Dish.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class Dish {
2323
private String description;
2424
private boolean delivered = false;
2525

26+
public static Dish deliver(Dish dish) {
27+
Dish deliveredDish = new Dish(dish.description);
28+
deliveredDish.delivered = true;
29+
return deliveredDish;
30+
}
31+
2632
Dish(String description) {
2733
this.description = description;
2834
}
@@ -39,10 +45,6 @@ public boolean isDelivered() {
3945
return delivered;
4046
}
4147

42-
public void setDelivered(boolean delivered) {
43-
this.delivered = delivered;
44-
}
45-
4648
@Override
4749
public String toString() {
4850
return "Dish{" + //

1-reactive/src/main/java/com/greglturnquist/hackingspringboot/reactive/ServerController.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@ Flux<Dish> serveDishes() {
4040
@GetMapping(value = "/served-dishes", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
4141
Flux<Dish> deliverDishes() {
4242
return this.kitchen.getDishes() //
43-
.map(dish -> deliver(dish));
44-
}
45-
46-
private Dish deliver(Dish dish) {
47-
dish.setDelivered(true);
48-
return dish;
43+
.map(dish -> Dish.deliver(dish));
4944
}
5045
// end::deliver[]
5146
}

1-reactive/src/test/java/com/greglturnquist/hackingspringboot/reactive/DishMetaphor.java

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,9 @@ class SimpleServer {
4848

4949
Flux<Dish> doingMyJob() {
5050
return this.kitchen.getDishes() //
51-
.map(dish -> this.deliver(dish));
51+
.map(dish -> Dish.deliver(dish));
5252
}
5353

54-
Dish deliver(Dish dish) {
55-
dish.setDelivered(true);
56-
return dish;
57-
}
5854
}
5955
// end::simple-server[]
6056

@@ -74,13 +70,9 @@ Flux<Dish> doingMyJob() {
7470
.doOnError(error -> System.out.println("So sorry about " //
7571
+ error.getMessage())) //
7672
.doOnComplete(() -> System.out.println("Thanks for all your hard work!")) //
77-
.map(this::deliver);
73+
.map(Dish::deliver);
7874
}
7975

80-
Dish deliver(Dish dish) {
81-
dish.setDelivered(true);
82-
return dish;
83-
}
8476
}
8577
// end::polite-server[]
8678

@@ -105,8 +97,7 @@ Flux<Dish> doingMyJob() {
10597
}
10698

10799
Dish deliver(Dish dish) {
108-
dish.setDelivered(true);
109-
return dish;
100+
return Dish.deliver(dish);
110101
}
111102
}
112103

@@ -131,8 +122,7 @@ Flux<Dish> doingMyJob() {
131122
}
132123

133124
Dish deliver(Dish dish) {
134-
dish.setDelivered(true);
135-
return dish;
125+
return Dish.deliver(dish);
136126
}
137127
}
138128

@@ -142,6 +132,12 @@ class Dish {
142132
private String description;
143133
private boolean delivered = false;
144134

135+
public static Dish deliver(Dish dish) {
136+
Dish deliveredDish = new Dish(dish.description);
137+
deliveredDish.delivered = true;
138+
return deliveredDish;
139+
}
140+
145141
Dish(String description) {
146142
this.description = description;
147143
}
@@ -150,10 +146,6 @@ public boolean isDelivered() {
150146
return delivered;
151147
}
152148

153-
public void setDelivered(boolean delivered) {
154-
this.delivered = delivered;
155-
}
156-
157149
@Override
158150
public String toString() {
159151
return "Dish{" + //

2-reactive/src/main/java/com/greglturnquist/hackingspringboot/reactive/HomeController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ Mono<String> addToCart(@PathVariable String id) { // <2>
7878
});
7979
}))
8080
.flatMap(cart -> this.cartRepository.save(cart)) // <6>
81-
.then(Mono.just("redirect:/")); // <7>
81+
.thenReturn("redirect:/"); // <7>
8282
}
8383
// end::3[]
8484

8585
@PostMapping
8686
Mono<String> createItem(@ModelAttribute Item newItem) {
8787
return this.itemRepository.save(newItem) //
88-
.then(Mono.just("redirect:/"));
88+
.thenReturn("redirect:/");
8989
}
9090

9191
@DeleteMapping("/delete/{id}")
9292
Mono<String> deleteItem(@PathVariable String id) {
9393
return this.itemRepository.deleteById(id) //
94-
.then(Mono.just("redirect:/"));
94+
.thenReturn("redirect:/");
9595
}
9696
}

2-reactive/src/test/java/com/greglturnquist/hackingspringboot/reactive/HomeController2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public HomeController2(CartService cartService) {
3838
@PostMapping("/add/{id}")
3939
Mono<String> addToCart(@PathVariable String id) {
4040
return this.cartService.addToCart("My Cart", id) //
41-
.then(Mono.just("redirect:/"));
41+
.thenReturn("redirect:/");
4242
}
4343
// end::4[]
4444

2b-reactive/src/main/java/com/greglturnquist/hackingspringboot/reactive/HomeController.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,20 +80,20 @@ Mono<String> addToCart(@PathVariable String id) { // <2>
8080
});
8181
}))
8282
.flatMap(cart -> this.cartRepository.save(cart)) // <6>
83-
.then(Mono.just("redirect:/")); // <7>
83+
.thenReturn("redirect:/"); // <7>
8484
}
8585
// end::3[]
8686

8787
@PostMapping
8888
Mono<String> createItem(@ModelAttribute Item newItem) {
8989
return this.itemRepository.save(newItem) //
90-
.then(Mono.just("redirect:/"));
90+
.thenReturn("redirect:/");
9191
}
9292

9393
@DeleteMapping("/delete/{id}")
9494
Mono<String> deleteItem(@PathVariable String id) {
9595
return this.itemRepository.deleteById(id) //
96-
.then(Mono.just("redirect:/"));
96+
.thenReturn("redirect:/");
9797
}
9898

9999
// tag::search[]

2b-reactive/src/test/java/com/greglturnquist/hackingspringboot/reactive/HomeController2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public HomeController2(CartService cartService) {
4040
@PostMapping("/add/{id}")
4141
Mono<String> addToCart(@PathVariable String id) {
4242
return this.cartService.addToCart("My Cart", id) //
43-
.then(Mono.just("redirect:/"));
43+
.thenReturn("redirect:/");
4444
}
4545
// end::4[]
4646

3-reactive/src/main/java/com/greglturnquist/hackingspringboot/reactive/HomeController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,24 @@ Mono<Rendering> home() {
5353
@PostMapping("/add/{id}")
5454
Mono<String> addToCart(@PathVariable String id) {
5555
return this.inventoryService.addItemToCart("My Cart", id)
56-
.then(Mono.just("redirect:/"));
56+
.thenReturn("redirect:/");
5757
}
5858

5959
@DeleteMapping("/remove/{id}")
6060
Mono<String> removeFromCart(@PathVariable String id) {
6161
return this.inventoryService.removeOneFromCart("My Cart", id)
62-
.then(Mono.just("redirect:/"));
62+
.thenReturn("redirect:/");
6363
}
6464

6565
@PostMapping
6666
Mono<String> createItem(@ModelAttribute Item newItem) {
6767
return this.inventoryService.saveItem(newItem) //
68-
.then(Mono.just("redirect:/"));
68+
.thenReturn("redirect:/");
6969
}
7070

7171
@DeleteMapping("/delete/{id}")
7272
Mono<String> deleteItem(@PathVariable String id) {
7373
return this.inventoryService.deleteItem(id) //
74-
.then(Mono.just("redirect:/"));
74+
.thenReturn("redirect:/");
7575
}
7676
}

3-reactive/src/test/java/com/greglturnquist/hackingspringboot/reactive/DebuggingReactorFlows.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static void main(String[] args) {
4949
}
5050

5151
try {
52-
executor.submit(() -> source.get(5)).get(); // line 46
52+
executor.submit(() -> source.get(5)).get(); // line 52
5353
} catch (InterruptedException | ExecutionException e) {
5454
e.printStackTrace();
5555
} finally {
@@ -71,7 +71,7 @@ public static void main(String[] args) {
7171

7272
source //
7373
.subscribeOn(Schedulers.parallel()) //
74-
.block(); // line 73
74+
.block(); // line 74
7575
}
7676
}
7777
// end::reactor-example[]
@@ -86,11 +86,11 @@ public static void main(String[] args) {
8686
if (new Random().nextBoolean()) {
8787
source = Flux.range(1, 10).elementAt(5);
8888
} else {
89-
source = Flux.just(1, 2, 3, 4).elementAt(5); // line 86
89+
source = Flux.just(1, 2, 3, 4).elementAt(5); // line 89
9090
}
9191
source //
9292
.subscribeOn(Schedulers.parallel()) //
93-
.block(); // line 88
93+
.block(); // line 93
9494
}
9595
}
9696
// end::reactor-example2[]

4-reactive/src/main/java/com/greglturnquist/hackingspringboot/reactive/HomeController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,24 @@ Mono<Rendering> home() { // <1>
5353
@PostMapping("/add/{id}")
5454
Mono<String> addToCart(@PathVariable String id) {
5555
return this.inventoryService.addItemToCart("My Cart", id)
56-
.then(Mono.just("redirect:/"));
56+
.thenReturn("redirect:/");
5757
}
5858

5959
@DeleteMapping("/remove/{id}")
6060
Mono<String> removeFromCart(@PathVariable String id) {
6161
return this.inventoryService.removeOneFromCart("My Cart", id)
62-
.then(Mono.just("redirect:/"));
62+
.thenReturn("redirect:/");
6363
}
6464

6565
@PostMapping
6666
Mono<String> createItem(@ModelAttribute Item newItem) {
6767
return this.inventoryService.saveItem(newItem) //
68-
.then(Mono.just("redirect:/"));
68+
.thenReturn("redirect:/");
6969
}
7070

7171
@DeleteMapping("/delete/{id}")
7272
Mono<String> deleteItem(@PathVariable String id) {
7373
return this.inventoryService.deleteItem(id) //
74-
.then(Mono.just("redirect:/"));
74+
.thenReturn("redirect:/");
7575
}
7676
}

5-reactive/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,6 @@
5252
<scope>runtime</scope>
5353
</dependency>
5454

55-
<dependency>
56-
<groupId>io.projectreactor.tools</groupId>
57-
<artifactId>blockhound-junit-platform</artifactId>
58-
<version>1.0.3.RELEASE</version>
59-
<scope>test</scope>
60-
</dependency>
6155
<dependency>
6256
<groupId>org.springframework.boot</groupId>
6357
<artifactId>spring-boot-starter-test</artifactId>

5-reactive/src/main/java/com/greglturnquist/hackingspringboot/reactive/HomeController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,24 @@ Mono<Rendering> home() { // <1>
5353
@PostMapping("/add/{id}")
5454
Mono<String> addToCart(@PathVariable String id) {
5555
return this.inventoryService.addItemToCart("My Cart", id)
56-
.then(Mono.just("redirect:/"));
56+
.thenReturn("redirect:/");
5757
}
5858

5959
@DeleteMapping("/remove/{id}")
6060
Mono<String> removeFromCart(@PathVariable String id) {
6161
return this.inventoryService.removeOneFromCart("My Cart", id)
62-
.then(Mono.just("redirect:/"));
62+
.thenReturn("redirect:/");
6363
}
6464

6565
@PostMapping
6666
Mono<String> createItem(@ModelAttribute Item newItem) {
6767
return this.inventoryService.saveItem(newItem) //
68-
.then(Mono.just("redirect:/"));
68+
.thenReturn("redirect:/");
6969
}
7070

7171
@DeleteMapping("/delete/{id}")
7272
Mono<String> deleteItem(@PathVariable String id) {
7373
return this.inventoryService.deleteItem(id) //
74-
.then(Mono.just("redirect:/"));
74+
.thenReturn("redirect:/");
7575
}
7676
}

6-reactive/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@
6666
</dependency>
6767
<!-- end::restdocs[] -->
6868

69-
<dependency>
70-
<groupId>io.projectreactor.tools</groupId>
71-
<artifactId>blockhound-junit-platform</artifactId>
72-
<version>1.0.3.RELEASE</version>
73-
<scope>test</scope>
74-
</dependency>
7569
<dependency>
7670
<groupId>org.springframework.boot</groupId>
7771
<artifactId>spring-boot-starter-test</artifactId>

6-reactive/src/main/java/com/greglturnquist/hackingspringboot/reactive/AffordancesItemController.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ Mono<EntityModel<Item>> findOne(@PathVariable String id) {
107107
.withRel(IanaLinkRelations.ITEM) //
108108
.toMono();
109109

110-
return selfLink.zipWith(aggregateLink) //
111-
.map(lnks -> Links.of(lnks.getT1(), lnks.getT2())) //
112-
.flatMap(links -> this.repository.findById(id) //
113-
.map(item -> EntityModel.of(item, links)));
110+
return Mono.zip(repository.findById(id), selfLink, aggregateLink) //
111+
.map(o -> EntityModel.of(o.getT1(), Links.of(o.getT2(), o.getT3())));
114112
}
115113
// end::find-one[]
116114

6-reactive/src/main/java/com/greglturnquist/hackingspringboot/reactive/ApiItemController.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ public Mono<ResponseEntity<?>> updateItem( //
7878
.map(content -> new Item(id, content.getName(), content.getDescription(), //
7979
content.getPrice())) // <4>
8080
.flatMap(this.repository::save) // <5>
81-
.then(Mono.just(ResponseEntity.created( // <6>
82-
URI.create("/api/items/" + id)).build()));
81+
.thenReturn(ResponseEntity.created( // <6>
82+
URI.create("/api/items/" + id)).build());
8383
}
8484
// end::replace-item[]
8585
}

6-reactive/src/main/java/com/greglturnquist/hackingspringboot/reactive/HypermediaItemController.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,8 @@ Mono<EntityModel<Item>> findOne(@PathVariable String id) {
9898
Mono<Link> aggregateLink = linkTo(controller.findAll()) //
9999
.withRel(IanaLinkRelations.ITEM).toMono(); // <3>
100100

101-
return selfLink.zipWith(aggregateLink) // <4>
102-
.map(links -> Links.of(links.getT1(), links.getT2())) // <5>
103-
.flatMap(links -> this.repository.findById(id) // <6>
104-
.map(item -> EntityModel.of(item, links))); // <7>
101+
return Mono.zip(repository.findById(id), selfLink, aggregateLink) // <4>
102+
.map(o -> EntityModel.of(o.getT1(), Links.of(o.getT2(), o.getT3()))); // <5>
105103
}
106104
// end::find-one[]
107105

@@ -119,10 +117,8 @@ Mono<EntityModel<Item>> findOneWithAffordances(@PathVariable String id) {
119117
Mono<Link> aggregateLink = linkTo(controller.findAll()).withRel(IanaLinkRelations.ITEM) //
120118
.toMono();
121119

122-
return selfLink.zipWith(aggregateLink) //
123-
.map(links -> Links.of(links.getT1(), links.getT2())) //
124-
.flatMap(links -> this.repository.findById(id) //
125-
.map(item -> EntityModel.of(item, links))); //
120+
return Mono.zip(repository.findById(id), selfLink, aggregateLink) //
121+
.map(o -> EntityModel.of(o.getT1(), Links.of(o.getT2(), o.getT3())));
126122
}
127123
// end::find-affordance[]
128124

0 commit comments

Comments
 (0)