Skip to content

Commit c1eb852

Browse files
committed
Use proper HTTP verbs.
1 parent 26cbe29 commit c1eb852

File tree

37 files changed

+190
-81
lines changed

37 files changed

+190
-81
lines changed

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

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,11 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.context.annotation.Bean;
6-
import org.springframework.web.filter.reactive.HiddenHttpMethodFilter;
75

86
@SpringBootApplication
97
public class HackingSpringBootApplication {
108

119
public static void main(String[] args) {
1210
SpringApplication.run(HackingSpringBootApplication.class, args);
1311
}
14-
15-
@Bean
16-
HiddenHttpMethodFilter hiddenHttpMethodFilter() {
17-
return new HiddenHttpMethodFilter();
18-
}
1912
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.greglturnquist.hackingspringboot.reactive;
1818

19+
import org.springframework.web.bind.annotation.DeleteMapping;
1920
import reactor.core.publisher.Mono;
2021

2122
import org.springframework.stereotype.Controller;
@@ -56,7 +57,7 @@ Mono<Rendering> home() { // <1>
5657
// end::2[]
5758

5859
// tag::3[]
59-
@GetMapping("/add/{id}") // <1>
60+
@PostMapping("/add/{id}") // <1>
6061
Mono<String> addToCart(@PathVariable String id) { // <2>
6162
return this.cartRepository.findById("My Cart") //
6263
.defaultIfEmpty(new Cart("My Cart")) // <3>
@@ -87,7 +88,7 @@ Mono<String> createItem(@ModelAttribute Item newItem) {
8788
.then(Mono.just("redirect:/"));
8889
}
8990

90-
@GetMapping("/delete/{id}")
91+
@DeleteMapping("/delete/{id}")
9192
Mono<String> deleteItem(@PathVariable String id) {
9293
return this.itemRepository.deleteById(id) //
9394
.then(Mono.just("redirect:/"));
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
logging.level.org.springframework.data=TRACE
1+
logging.level.org.springframework.data=TRACE
2+
# tag::hidden[]
3+
spring.webflux.hiddenmethod.filter.enabled=true
4+
# end::hidden[]

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@ <h2>Inventory Management</h2>
2222
<td th:text="${item.id}"></td>
2323
<td th:text="${item.name}"></td>
2424
<td th:text="${item.price}"></td>
25-
<td><a th:href="@{'/add/' + ${item.id}}">Add to Cart</a></td>
26-
<td><a th:href="@{'/delete/' + ${item.id}}">Delete</a></td>
25+
<td>
26+
<form method="post" th:action="@{'/add/' + ${item.id}}">
27+
<input type="submit" value="Add to Cart" />
28+
</form>
29+
</td>
30+
<td>
31+
<form th:method="delete" th:action="@{'/delete/' + ${item.id}}">
32+
<input type="submit" value="Delete"/>
33+
</form>
34+
</td>
2735
</tr>
2836
</tbody>
2937
</table>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.greglturnquist.hackingspringboot.reactive;
1818

19+
import org.springframework.web.bind.annotation.PostMapping;
1920
import reactor.core.publisher.Mono;
2021
import org.springframework.stereotype.Controller;
2122
import org.springframework.web.bind.annotation.GetMapping;
@@ -34,7 +35,7 @@ public HomeController2(CartService cartService) {
3435
}
3536

3637
// tag::4[]
37-
@GetMapping("/add/{id}")
38+
@PostMapping("/add/{id}")
3839
Mono<String> addToCart(@PathVariable String id) {
3940
return this.cartService.addToCart("My Cart", id) //
4041
.then(Mono.just("redirect:/"));

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,4 @@ public class HackingSpringBootApplication {
1111
public static void main(String[] args) {
1212
SpringApplication.run(HackingSpringBootApplication.class, args);
1313
}
14-
15-
@Bean
16-
HiddenHttpMethodFilter hiddenHttpMethodFilter() {
17-
return new HiddenHttpMethodFilter();
18-
}
1914
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.greglturnquist.hackingspringboot.reactive;
1818

19+
import org.springframework.web.bind.annotation.DeleteMapping;
1920
import reactor.core.publisher.Mono;
2021
import org.springframework.stereotype.Controller;
2122
import org.springframework.web.bind.annotation.GetMapping;
@@ -58,7 +59,7 @@ Mono<Rendering> home() { // <1>
5859
// end::2[]
5960

6061
// tag::3[]
61-
@GetMapping("/add/{id}")
62+
@PostMapping("/add/{id}")
6263
// <1>
6364
Mono<String> addToCart(@PathVariable String id) { // <2>
6465
return this.cartRepository.findById("My Cart") // <3>
@@ -89,7 +90,7 @@ Mono<String> createItem(@ModelAttribute Item newItem) {
8990
.then(Mono.just("redirect:/"));
9091
}
9192

92-
@GetMapping("/delete/{id}")
93+
@DeleteMapping("/delete/{id}")
9394
Mono<String> deleteItem(@PathVariable String id) {
9495
return this.itemRepository.deleteById(id) //
9596
.then(Mono.just("redirect:/"));
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
logging.level.org.springframework.data=TRACE
1+
logging.level.org.springframework.data=TRACE
2+
# tag::hidden[]
3+
spring.webflux.hiddenmethod.filter.enabled=true
4+
# end::hidden[]

2b-reactive/src/main/resources/templates/home.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ <h2>Inventory Management</h2>
1616
<td th:text="${item.id}"></td>
1717
<td th:text="${item.name}"></td>
1818
<td th:text="${item.price}"></td>
19-
<td><a th:href="@{'/add/' + ${item.id}}">Add to Cart</a></td>
20-
<td><a th:href="@{'/delete/' + ${item.id}}">Delete</a></td>
19+
<td>
20+
<form method="post" th:action="@{'/add/' + ${item.id}}">
21+
<input type="submit" value="Add to Cart" />
22+
</form>
23+
</td>
24+
<td>
25+
<form th:method="delete" th:action="@{'/delete/' + ${item.id}}">
26+
<input type="submit" value="Delete"/>
27+
</form>
28+
</td>
2129
</tr>
2230
</tbody>
2331
</table>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.stereotype.Controller;
2020
import org.springframework.web.bind.annotation.GetMapping;
2121
import org.springframework.web.bind.annotation.PathVariable;
22+
import org.springframework.web.bind.annotation.PostMapping;
2223
import reactor.core.publisher.Mono;
2324

2425
import com.greglturnquist.hackingspringboot.reactive.CartService;
@@ -36,7 +37,7 @@ public HomeController2(CartService cartService) {
3637
}
3738

3839
// tag::4[]
39-
@GetMapping("/add/{id}")
40+
@PostMapping("/add/{id}")
4041
Mono<String> addToCart(@PathVariable String id) {
4142
return this.cartService.addToCart("My Cart", id) //
4243
.then(Mono.just("redirect:/"));

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,4 @@ public static void main(String[] args) {
2121
SpringApplication.run(HackingSpringBootApplicationBlockHoundCustomized.class, args);
2222
}
2323
// end::blockhound[]
24-
25-
@Bean
26-
HiddenHttpMethodFilter hiddenHttpMethodFilter() {
27-
return new HiddenHttpMethodFilter();
28-
}
2924
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.greglturnquist.hackingspringboot.reactive;
1818

19+
import org.springframework.web.bind.annotation.DeleteMapping;
1920
import reactor.core.publisher.Mono;
2021
import org.springframework.stereotype.Controller;
2122
import org.springframework.web.bind.annotation.GetMapping;
@@ -49,13 +50,13 @@ Mono<Rendering> home() {
4950
// end::2[]
5051
}
5152

52-
@GetMapping("/add/{id}")
53+
@PostMapping("/add/{id}")
5354
Mono<String> addToCart(@PathVariable String id) {
5455
return this.inventoryService.addItemToCart("My Cart", id)
5556
.then(Mono.just("redirect:/"));
5657
}
5758

58-
@GetMapping("/remove/{id}")
59+
@DeleteMapping("/remove/{id}")
5960
Mono<String> removeFromCart(@PathVariable String id) {
6061
return this.inventoryService.removeOneFromCart("My Cart", id)
6162
.then(Mono.just("redirect:/"));
@@ -67,7 +68,7 @@ Mono<String> createItem(@ModelAttribute Item newItem) {
6768
.then(Mono.just("redirect:/"));
6869
}
6970

70-
@GetMapping("/delete/{id}")
71+
@DeleteMapping("/delete/{id}")
7172
Mono<String> deleteItem(@PathVariable String id) {
7273
return this.inventoryService.deleteItem(id) //
7374
.then(Mono.just("redirect:/"));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
logging.level.com.gregturnquist=DEBUG
22
logging.level.web=DEBUG
3+
spring.webflux.hiddenmethod.filter.enabled=true

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ <h2>Inventory Management</h2>
1616
<td th:text="${item.id}"></td>
1717
<td th:text="${item.name}"></td>
1818
<td th:text="${item.price}"></td>
19-
<td><a th:href="@{'/add/' + ${item.id}}">Add to Cart</a></td>
20-
<td><a th:href="@{'/delete/' + ${item.id}}">Delete</a></td>
19+
<td>
20+
<form method="post" th:action="@{'/add/' + ${item.id}}">
21+
<input type="submit" value="Add to Cart" />
22+
</form>
23+
</td>
24+
<td>
25+
<form th:method="delete" th:action="@{'/delete/' + ${item.id}}">
26+
<input type="submit" value="Delete"/>
27+
</form>
28+
</td>
2129
</tr>
2230
</tbody>
2331
</table>
@@ -32,7 +40,11 @@ <h2>My Cart</h2>
3240
<td th:text="${cartItem.item.id}"></td>
3341
<td th:text="${cartItem.item.name}"></td>
3442
<td th:text="${cartItem.quantity}"></td>
35-
<td><a th:href="@{'/remove/' + ${cartItem.item.id}}">-1</a></td>
43+
<td>
44+
<form th:method="delete" th:action="@{'/remove/' + ${cartItem.item.id}}">
45+
<input type="submit" value="-1"/>
46+
</form>
47+
</td>
3648
</tr>
3749
</tbody>
3850
</table>

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.greglturnquist.hackingspringboot.reactive;
1818

1919
import org.springframework.stereotype.Controller;
20+
import org.springframework.web.bind.annotation.DeleteMapping;
2021
import org.springframework.web.bind.annotation.GetMapping;
2122
import org.springframework.web.bind.annotation.ModelAttribute;
2223
import org.springframework.web.bind.annotation.PathVariable;
@@ -49,13 +50,13 @@ Mono<Rendering> home() { // <1>
4950
}
5051
// end::2[]
5152

52-
@GetMapping("/add/{id}")
53+
@PostMapping("/add/{id}")
5354
Mono<String> addToCart(@PathVariable String id) {
5455
return this.inventoryService.addItemToCart("My Cart", id)
5556
.then(Mono.just("redirect:/"));
5657
}
5758

58-
@GetMapping("/remove/{id}")
59+
@DeleteMapping("/remove/{id}")
5960
Mono<String> removeFromCart(@PathVariable String id) {
6061
return this.inventoryService.removeOneFromCart("My Cart", id)
6162
.then(Mono.just("redirect:/"));
@@ -67,7 +68,7 @@ Mono<String> createItem(@ModelAttribute Item newItem) {
6768
.then(Mono.just("redirect:/"));
6869
}
6970

70-
@GetMapping("/delete/{id}")
71+
@DeleteMapping("/delete/{id}")
7172
Mono<String> deleteItem(@PathVariable String id) {
7273
return this.inventoryService.deleteItem(id) //
7374
.then(Mono.just("redirect:/"));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
spring.webflux.hiddenmethod.filter.enabled=true

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ <h2>Inventory Management</h2>
1616
<td th:text="${item.id}"></td>
1717
<td th:text="${item.name}"></td>
1818
<td th:text="${item.price}"></td>
19-
<td><a th:href="@{'/add/' + ${item.id}}">Add to Cart</a></td>
20-
<td><a th:href="@{'/delete/' + ${item.id}}">Delete</a></td>
19+
<td>
20+
<form method="post" th:action="@{'/add/' + ${item.id}}">
21+
<input type="submit" value="Add to Cart" />
22+
</form>
23+
</td>
24+
<td>
25+
<form th:method="delete" th:action="@{'/delete/' + ${item.id}}">
26+
<input type="submit" value="Delete"/>
27+
</form>
28+
</td>
2129
</tr>
2230
</tbody>
2331
</table>
@@ -32,7 +40,11 @@ <h2>My Cart</h2>
3240
<td th:text="${cartItem.item.id}"></td>
3341
<td th:text="${cartItem.item.name}"></td>
3442
<td th:text="${cartItem.quantity}"></td>
35-
<td><a th:href="@{'/remove/' + ${cartItem.item.id}}">-1</a></td>
43+
<td>
44+
<form th:method="delete" th:action="@{'/remove/' + ${cartItem.item.id}}">
45+
<input type="submit" value="-1"/>
46+
</form>
47+
</td>
3648
</tr>
3749
</tbody>
3850
</table>

4-reactive/src/test/java/com/greglturnquist/hackingspringboot/reactive/HomeControllerSliceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ void homePage() {
5555
.expectBody(String.class) //
5656
.consumeWith(exchangeResult -> {
5757
assertThat( //
58-
exchangeResult.getResponseBody()).contains("<a href=\"/add/id1\">");
58+
exchangeResult.getResponseBody()).contains("action=\"/add/id1\"");
5959
assertThat( //
60-
exchangeResult.getResponseBody()).contains("<a href=\"/add/id2\">");
60+
exchangeResult.getResponseBody()).contains("action=\"/add/id2\"");
6161
});
6262
}
6363
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.greglturnquist.hackingspringboot.reactive;
1818

1919
import org.springframework.stereotype.Controller;
20+
import org.springframework.web.bind.annotation.DeleteMapping;
2021
import org.springframework.web.bind.annotation.GetMapping;
2122
import org.springframework.web.bind.annotation.ModelAttribute;
2223
import org.springframework.web.bind.annotation.PathVariable;
@@ -49,13 +50,13 @@ Mono<Rendering> home() { // <1>
4950
}
5051
// end::2[]
5152

52-
@GetMapping("/add/{id}")
53+
@PostMapping("/add/{id}")
5354
Mono<String> addToCart(@PathVariable String id) {
5455
return this.inventoryService.addItemToCart("My Cart", id)
5556
.then(Mono.just("redirect:/"));
5657
}
5758

58-
@GetMapping("/remove/{id}")
59+
@DeleteMapping("/remove/{id}")
5960
Mono<String> removeFromCart(@PathVariable String id) {
6061
return this.inventoryService.removeOneFromCart("My Cart", id)
6162
.then(Mono.just("redirect:/"));
@@ -67,7 +68,7 @@ Mono<String> createItem(@ModelAttribute Item newItem) {
6768
.then(Mono.just("redirect:/"));
6869
}
6970

70-
@GetMapping("/delete/{id}")
71+
@DeleteMapping("/delete/{id}")
7172
Mono<String> deleteItem(@PathVariable String id) {
7273
return this.inventoryService.deleteItem(id) //
7374
.then(Mono.just("redirect:/"));

5-reactive/src/main/resources/application.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ info.java.version=@java.version@
1313
info.spring.framework.version=@spring-framework.version@
1414
info.spring.data.version=@spring-data-releasetrain.version@
1515
# end::info[]
16+
spring.webflux.hiddenmethod.filter.enabled=true

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,16 @@ <h2>Inventory Management</h2>
1616
<td th:text="${item.id}"></td>
1717
<td th:text="${item.name}"></td>
1818
<td th:text="${item.price}"></td>
19-
<td><a th:href="@{'/add/' + ${item.id}}">Add to Cart</a></td>
20-
<td><a th:href="@{'/delete/' + ${item.id}}">Delete</a></td>
19+
<td>
20+
<form method="post" th:action="@{'/add/' + ${item.id}}">
21+
<input type="submit" value="Add to Cart" />
22+
</form>
23+
</td>
24+
<td>
25+
<form th:method="delete" th:action="@{'/delete/' + ${item.id}}">
26+
<input type="submit" value="Delete"/>
27+
</form>
28+
</td>
2129
</tr>
2230
</tbody>
2331
</table>
@@ -32,7 +40,11 @@ <h2>My Cart</h2>
3240
<td th:text="${cartItem.item.id}"></td>
3341
<td th:text="${cartItem.item.name}"></td>
3442
<td th:text="${cartItem.quantity}"></td>
35-
<td><a th:href="@{'/remove/' + ${cartItem.item.id}}">-1</a></td>
43+
<td>
44+
<form th:method="delete" th:action="@{'/remove/' + ${cartItem.item.id}}">
45+
<input type="submit" value="-1"/>
46+
</form>
47+
</td>
3648
</tr>
3749
</tbody>
3850
</table>

0 commit comments

Comments
 (0)