Skip to content

Commit bee0953

Browse files
committed
Proofreading up to ch 6.
1 parent 0249b18 commit bee0953

File tree

74 files changed

+1188
-969
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+1188
-969
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
@@ -16,6 +16,8 @@
1616
// tag::dish[]
1717
package com.greglturnquist.hackingspringboot.reactive;
1818

19+
import java.util.Objects;
20+
1921
class Dish {
2022

2123
private String description;
@@ -43,10 +45,10 @@ public void setDelivered(boolean delivered) {
4345

4446
@Override
4547
public String toString() {
46-
return "Dish{" +
47-
"description='" + description + '\'' +
48-
", delivered=" + delivered +
49-
'}';
48+
return "Dish{" + //
49+
"description='" + description + '\'' + //
50+
", delivered=" + delivered + //
51+
'}';
5052
}
5153
}
5254
// end::dish[]

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55

6+
// tag::code[]
67
@SpringBootApplication
78
public class HackingSpringBootApplication {
89

910
public static void main(String[] args) {
10-
SpringApplication.run(HackingSpringBootApplication.class, args);
11+
SpringApplication.run( //
12+
HackingSpringBootApplication.class, args);
1113
}
12-
1314
}
14-
15+
// end::code[]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
public class HomeController {
2626

2727
@GetMapping
28-
Mono<String> home(Model model) {
29-
return Mono.just("home.html");
28+
Mono<String> home() {
29+
return Mono.just("home");
3030
}
3131
}
3232
// end::code[]

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ public class KitchenService {
3131
* Generates continuous stream of dishes.
3232
*/
3333
Flux<Dish> getDishes() {
34-
return Flux
35-
.<Dish> generate(sink -> sink.next(randomDish()))
36-
.delayElements(Duration.ofMillis(250));
34+
return Flux //
35+
.<Dish> generate(sink -> sink.next(randomDish())) //
36+
.delayElements(Duration.ofMillis(250));
3737
}
3838

3939
/**
@@ -43,10 +43,10 @@ private Dish randomDish() {
4343
return menu.get(picker.nextInt(menu.size()));
4444
}
4545

46-
private List<Dish> menu = Arrays.asList(
47-
new Dish("Sesame chicken"),
48-
new Dish("Lo mein noodles, plain"),
49-
new Dish("Sweet & sour beef"));
46+
private List<Dish> menu = Arrays.asList( //
47+
new Dish("Sesame chicken"), //
48+
new Dish("Lo mein noodles, plain"), //
49+
new Dish("Sweet & sour beef"));
5050

5151
private Random picker = new Random();
5252
}

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,20 @@ public class ServerController {
2929
public ServerController(KitchenService kitchen) {
3030
this.kitchen = kitchen;
3131
}
32-
33-
@GetMapping(value = "/server", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
32+
33+
@GetMapping(value = "/server", //
34+
produces = MediaType.TEXT_EVENT_STREAM_VALUE)
3435
Flux<Dish> serveDishes() {
3536
return this.kitchen.getDishes();
3637
}
3738
// end::controller[]
3839

3940
// tag::deliver[]
40-
@GetMapping(value = "/served-dishes", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
41+
@GetMapping(value = "/served-dishes", //
42+
produces = MediaType.TEXT_EVENT_STREAM_VALUE)
4143
Flux<Dish> deliverDishes() {
42-
return this.kitchen.getDishes()
43-
.map(dish -> deliver(dish));
44+
return this.kitchen.getDishes() //
45+
.map(dish -> deliver(dish));
4446
}
4547

4648
private Dish deliver(Dish dish) {

1-reactive/src/main/resources/templates/home.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
<h1>Welcome to Hacking with Spring Boot!</h1>
99

1010
<p>
11-
Over the span of this book, we'll build
12-
an e-commerce system which will include
13-
fleshing out this web template with
14-
dynamic content, using the power of
15-
Spring Boot.
11+
Over the span of this book, you'll build
12+
different parts of an e-commerce system
13+
which will include fleshing out this web
14+
template with dynamic content, using the
15+
power of ˜Spring Boot.
1616
</p>
1717

1818
</body>

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

Lines changed: 63 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class KitchenService {
2929
Flux<Dish> getDishes() {
3030
// You could model a ChefService, but let's just
3131
// hard code some tasty dishes.
32-
return Flux.just(
33-
new Dish("Sesame chicken"),
34-
new Dish("Lo mein noodles, plain"),
32+
return Flux.just( //
33+
new Dish("Sesame chicken"), //
34+
new Dish("Lo mein noodles, plain"), //
3535
new Dish("Sweet & sour beef"));
3636
}
3737
}
@@ -47,7 +47,7 @@ class SimpleServer {
4747
}
4848

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

@@ -69,13 +69,26 @@ class PoliteServer {
6969
}
7070

7171
Flux<Dish> doingMyJob() {
72-
return this.kitchen.getDishes()
73-
.doOnNext(dish -> System.out.println("Thank you for " + dish + "!"))
74-
.doOnError(error -> System.out.println("So sorry about " + error.getMessage()))
75-
.doOnComplete(() -> System.out.println("Thanks for all your hard work!"))
72+
return this.kitchen.getDishes() //
73+
.doOnNext(dish -> thanks(dish)) //
74+
.doOnError(error -> sorry(error)) //
75+
.doOnComplete(() -> goodWork()) //
7676
.map(this::deliver);
7777
}
7878

79+
void thanks(Dish dish) {
80+
System.out.println("Thank you for " + dish + "!");
81+
}
82+
83+
void sorry(Throwable error) {
84+
System.out.println("So sorry about " //
85+
+ error.getMessage());
86+
}
87+
88+
void goodWork() {
89+
System.out.println("Thanks for all your hard work!");
90+
}
91+
7992
Dish deliver(Dish dish) {
8093
dish.setDelivered(true);
8194
return dish;
@@ -93,16 +106,28 @@ class BusyServer {
93106

94107
Flux<Dish> doingMyJob() {
95108
// tag::multiple-side-effects[]
96-
return this.kitchen.getDishes()
97-
.doOnNext(dish -> {
98-
System.out.println("Thank you for " + dish + "!");
99-
System.out.println("Marking the ticket as done.");
100-
System.out.println("Grabbing some silverware.");
101-
})
109+
return this.kitchen.getDishes() //
110+
.doOnNext(dish -> {
111+
thanks(dish);
112+
markAsDone();
113+
grabSomeSilverware();
114+
}) //
102115
.map(this::deliver);
103116
// end::multiple-side-effects[]
104117
}
105118

119+
void thanks(Dish dish) {
120+
System.out.println("Thank you for " + dish + "!");
121+
}
122+
123+
void markAsDone() {
124+
System.out.println("Marking the ticket as done.");
125+
}
126+
127+
void grabSomeSilverware() {
128+
System.out.println("Grabbing some silverware.");
129+
}
130+
106131
Dish deliver(Dish dish) {
107132
dish.setDelivered(true);
108133
return dish;
@@ -119,14 +144,26 @@ class BusyServer2 {
119144

120145
Flux<Dish> doingMyJob() {
121146
// tag::multiple-side-effects2[]
122-
return this.kitchen.getDishes()
123-
.doOnNext(dish -> System.out.println("Thank you for " + dish + "!"))
124-
.doOnNext(dish -> System.out.println("Marking the ticket as done."))
125-
.doOnNext(dish -> System.out.println("Grabbing some silverware."))
147+
return this.kitchen.getDishes() //
148+
.doOnNext(dish -> thanks(dish)) //
149+
.doOnNext(dish -> markAsDone()) //
150+
.doOnNext(dish -> grabSilverware()) //
126151
.map(this::deliver);
127152
// end::multiple-side-effects2[]
128153
}
129154

155+
void thanks(Dish dish) {
156+
System.out.println("Thank you for " + dish + "!");
157+
}
158+
159+
void markAsDone() {
160+
System.out.println("Marking the ticket as done.");
161+
}
162+
163+
void grabSilverware() {
164+
System.out.println("Grabbing some silverware.");
165+
}
166+
130167
Dish deliver(Dish dish) {
131168
dish.setDelivered(true);
132169
return dish;
@@ -153,10 +190,9 @@ public void setDelivered(boolean delivered) {
153190

154191
@Override
155192
public String toString() {
156-
return "Dish{" +
157-
"description='" + description + '\'' +
158-
", delivered=" + delivered +
159-
'}';
193+
return "Dish{" + //
194+
"description='" + description + '\'' + //
195+
", delivered=" + delivered + '}';
160196
}
161197
}
162198
// end::dish[]
@@ -166,12 +202,12 @@ public String toString() {
166202
class PoliteRestaurant {
167203

168204
public static void main(String... args) {
169-
PoliteServer server = new PoliteServer(new KitchenService());
205+
PoliteServer server = //
206+
new PoliteServer(new KitchenService());
170207

171-
server.doingMyJob() //
172-
.subscribe( //
173-
dish -> System.out.println("Consuming " + dish),
174-
throwable -> System.err.println(throwable));
208+
server.doingMyJob().subscribe( //
209+
dish -> System.out.println("Consuming " + dish), //
210+
throwable -> System.err.println(throwable));
175211
}
176212
}
177213
// end::example[]

2-reactive/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
<groupId>org.springframework.boot</groupId>
3737
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
3838
</dependency>
39-
<dependency>
40-
<groupId>org.mongodb</groupId>
41-
<artifactId>mongodb-driver-sync</artifactId>
42-
</dependency>
4339
<dependency>
4440
<groupId>de.flapdoodle.embed</groupId>
4541
<artifactId>de.flapdoodle.embed.mongo</artifactId>
4642
</dependency>
43+
<dependency>
44+
<groupId>org.mongodb</groupId>
45+
<artifactId>mongodb-driver-sync</artifactId>
46+
</dependency>
4747
<!-- end::mongodb[] -->
4848
<dependency>
4949
<groupId>org.springframework.boot</groupId>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
// tag::code[]
2828
class Cart {
2929

30-
@Id private String id;
30+
private @Id String id;
3131
private List<CartItem> cartItems;
3232

3333
private Cart() {}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
* @author Greg Turnquist
2222
*/
2323
// tag::code[]
24-
public interface CartRepository extends ReactiveCrudRepository<Cart, String> {
24+
public interface CartRepository extends //
25+
ReactiveCrudRepository<Cart, String> {
2526

2627
}
2728
// end::code[]

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,19 @@ Mono<Cart> addToCart(String cartId, String id) { // <3>
4040
return this.cartRepository.findById(cartId) //
4141
.defaultIfEmpty(new Cart(cartId)) //
4242
.flatMap(cart -> cart.getCartItems().stream() //
43-
.filter(cartItem -> cartItem.getItem().getId().equals(id)) //
43+
.filter(cartItem -> cartItem.getItem() //
44+
.getId().equals(id)) //
4445
.findAny() //
4546
.map(cartItem -> {
4647
cartItem.increment();
4748
return Mono.just(cart);
4849
}) //
49-
.orElseGet(() -> {
50-
return this.itemRepository.findById(id) //
51-
.map(CartItem::new) // <4>
52-
.doOnNext(cartItem -> cart.getCartItems().add(cartItem)) //
53-
.map(cartItem -> cart);
54-
}))
50+
.orElseGet(() -> //
51+
this.itemRepository.findById(id) //
52+
.map(CartItem::new) // <4>
53+
.doOnNext(cartItem -> //
54+
cart.getCartItems().add(cartItem)) //
55+
.map(cartItem -> cart)))
5556
.flatMap(this.cartRepository::save); // <5>
5657
}
5758
}

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,23 @@ public HomeController(ItemRepository itemRepository, // <2>
4646
@GetMapping
4747
Mono<Rendering> home() { // <1>
4848
return Mono.just(Rendering.view("home.html") // <2>
49-
.modelAttribute("items", this.itemRepository.findAll()) // <3>
50-
.modelAttribute("cart", this.cartRepository.findById("My Cart") // <4>
51-
.defaultIfEmpty(new Cart("My Cart")))
49+
.modelAttribute("items", //
50+
this.itemRepository.findAll()) // <3>
51+
.modelAttribute("cart", //
52+
this.cartRepository.findById("My Cart") // <4>
53+
.defaultIfEmpty(new Cart("My Cart")))
5254
.build());
5355
}
5456
// end::2[]
5557

5658
// tag::3[]
5759
@GetMapping("/add/{id}") // <1>
5860
Mono<String> addToCart(@PathVariable String id) { // <2>
59-
return this.cartRepository.findById("My Cart") // <3>
60-
.defaultIfEmpty(new Cart("My Cart")) //
61+
return this.cartRepository.findById("My Cart") //
62+
.defaultIfEmpty(new Cart("My Cart")) // <3>
6163
.flatMap(cart -> cart.getCartItems().stream() // <4>
62-
.filter(cartItem -> cartItem.getItem().getId().equals(id)) //
64+
.filter(cartItem -> cartItem.getItem() //
65+
.getId().equals(id)) //
6366
.findAny() //
6467
.map(cartItem -> {
6568
cartItem.increment();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// tag::code[]
2323
public class Item {
2424

25-
@Id private String id;
25+
private @Id String id;
2626
private String name;
2727
private double price;
2828

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
// tag::code[]
1716
package com.greglturnquist.hackingspringboot.reactive;
1817

18+
// tag::code[]
1919
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
2020

21-
public interface ItemRepository extends ReactiveCrudRepository<Item, String> {
21+
public interface ItemRepository extends //
22+
ReactiveCrudRepository<Item, String> {
2223

2324
}
2425
// end::code[]

0 commit comments

Comments
 (0)