Skip to content

Commit 7fe04d4

Browse files
committed
#833 - Cleanups in sample controllers for Spring WebMVC and WebFlux.
1 parent 5c046e6 commit 7fe04d4

File tree

2 files changed

+39
-33
lines changed

2 files changed

+39
-33
lines changed

src/test/java/org/springframework/hateoas/support/WebFluxEmployeeController.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
import java.util.TreeMap;
2828

2929
import org.springframework.hateoas.Affordance;
30+
import org.springframework.hateoas.CollectionModel;
31+
import org.springframework.hateoas.EntityModel;
3032
import org.springframework.hateoas.IanaLinkRelations;
3133
import org.springframework.hateoas.Link;
3234
import org.springframework.hateoas.Links;
33-
import org.springframework.hateoas.EntityModel;
34-
import org.springframework.hateoas.CollectionModel;
3535
import org.springframework.hateoas.server.reactive.WebFluxLinkBuilder;
3636
import org.springframework.http.ResponseEntity;
3737
import org.springframework.web.bind.annotation.GetMapping;
@@ -65,14 +65,14 @@ public static void reset() {
6565
@GetMapping("/employees")
6666
public Mono<CollectionModel<EntityModel<Employee>>> all() {
6767

68-
Class<WebFluxEmployeeController> controller = WebFluxEmployeeController.class;
68+
WebFluxEmployeeController controller = methodOn(WebFluxEmployeeController.class);
6969

7070
return Flux.fromIterable(EMPLOYEES.keySet()) //
7171
.flatMap(id -> findOne(id)) //
7272
.collectList() //
73-
.flatMap(resources -> linkTo(methodOn(controller).all()).withSelfRel() //
74-
.andAffordance(methodOn(controller).newEmployee(null)) //
75-
.andAffordance(methodOn(controller).search(null, null)) //
73+
.flatMap(resources -> linkTo(controller.all()).withSelfRel() //
74+
.andAffordance(controller.newEmployee(null)) //
75+
.andAffordance(controller.search(null, null)) //
7676
.toMono() //
7777
.map(selfLink -> new CollectionModel<>(resources, selfLink)));
7878
}
@@ -82,6 +82,8 @@ public Mono<CollectionModel<EntityModel<Employee>>> search( //
8282
@RequestParam Optional<String> name, //
8383
@RequestParam Optional<String> role) {
8484

85+
Class<WebFluxEmployeeController> controller = WebFluxEmployeeController.class;
86+
8587
return Flux.fromIterable(EMPLOYEES.keySet()) //
8688
.flatMap(id -> findOne(id)) //
8789
.filter(resource -> {
@@ -94,27 +96,24 @@ public Mono<CollectionModel<EntityModel<Employee>>> search( //
9496
.orElse(true);
9597

9698
return nameMatches && roleMatches;
97-
}).collectList().flatMap(resources -> {
98-
99-
Class<WebFluxEmployeeController> controller = WebFluxEmployeeController.class;
100-
101-
return linkTo(methodOn(controller).all()).withSelfRel() //
102-
.andAffordance(methodOn(controller).newEmployee(null)) //
103-
.andAffordance(methodOn(controller).search(null, null)) //
104-
.toMono() //
105-
.map(selfLink -> new CollectionModel<>(resources, selfLink));
106-
});
99+
}).collectList().flatMap(resources -> linkTo(methodOn(controller).all()).withSelfRel() //
100+
.andAffordance(methodOn(controller).newEmployee(null)) //
101+
.andAffordance(methodOn(controller).search(null, null)) //
102+
.toMono() //
103+
.map(selfLink -> new CollectionModel<>(resources, selfLink)));
107104
}
108105

109106
@GetMapping("/employees/{id}")
110107
public Mono<EntityModel<Employee>> findOne(@PathVariable Integer id) {
111108

112-
Mono<Link> selfLink = linkTo(methodOn(WebFluxEmployeeController.class).findOne(id)).withSelfRel() //
113-
.andAffordance(methodOn(WebFluxEmployeeController.class).updateEmployee(null, id)) //
114-
.andAffordance(methodOn(WebFluxEmployeeController.class).partiallyUpdateEmployee(null, id)) //
109+
WebFluxEmployeeController controller = methodOn(WebFluxEmployeeController.class);
110+
111+
Mono<Link> selfLink = linkTo(controller.findOne(id)).withSelfRel() //
112+
.andAffordance(controller.updateEmployee(null, id)) //
113+
.andAffordance(controller.partiallyUpdateEmployee(null, id)) //
115114
.toMono();
116115

117-
Mono<Link> employeesLink = linkTo(methodOn(WebFluxEmployeeController.class).all()).withRel("employees") //
116+
Mono<Link> employeesLink = linkTo(controller.all()).withRel("employees") //
118117
.toMono();
119118

120119
return selfLink.zipWith(employeesLink) //

src/test/java/org/springframework/hateoas/support/WebMvcEmployeeController.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
import java.util.stream.Collectors;
2828
import java.util.stream.IntStream;
2929

30+
import org.springframework.hateoas.CollectionModel;
31+
import org.springframework.hateoas.EntityModel;
3032
import org.springframework.hateoas.IanaLinkRelations;
3133
import org.springframework.hateoas.Link;
32-
import org.springframework.hateoas.EntityModel;
33-
import org.springframework.hateoas.CollectionModel;
3434
import org.springframework.http.ResponseEntity;
3535
import org.springframework.web.bind.annotation.GetMapping;
3636
import org.springframework.web.bind.annotation.PatchMapping;
@@ -61,9 +61,11 @@ public static void reset() {
6161
public CollectionModel<EntityModel<Employee>> all() {
6262

6363
// Generate an "Affordance" based on this method (the "self" link)
64-
Link selfLink = linkTo(methodOn(WebMvcEmployeeController.class).all()).withSelfRel() //
65-
.andAffordance(afford(methodOn(WebMvcEmployeeController.class).newEmployee(null))) //
66-
.andAffordance(afford(methodOn(WebMvcEmployeeController.class).search(null, null)));
64+
WebMvcEmployeeController controller = methodOn(WebMvcEmployeeController.class);
65+
66+
Link selfLink = linkTo(controller.all()).withSelfRel() //
67+
.andAffordance(afford(controller.newEmployee(null))) //
68+
.andAffordance(afford(controller.search(null, null)));
6769

6870
// Return the collection of employee resources along with the composite affordance
6971
return IntStream.range(0, EMPLOYEES.size()) //
@@ -97,10 +99,12 @@ public CollectionModel<EntityModel<Employee>> search(@RequestParam(value = "name
9799
}
98100

99101
// Generate an "Affordance" based on this method (the "self" link)
100-
Link selfLink = linkTo(methodOn(WebMvcEmployeeController.class).all()) //
102+
WebMvcEmployeeController controller = methodOn(WebMvcEmployeeController.class);
103+
104+
Link selfLink = linkTo(controller.all()) //
101105
.withSelfRel() //
102-
.andAffordance(afford(methodOn(WebMvcEmployeeController.class).newEmployee(null))) //
103-
.andAffordance(afford(methodOn(WebMvcEmployeeController.class).search(null, null)));
106+
.andAffordance(afford(controller.newEmployee(null))) //
107+
.andAffordance(afford(controller.search(null, null)));
104108

105109
// Return the collection of employee resources along with the composite affordance
106110
return new CollectionModel<>(employees, selfLink);
@@ -110,16 +114,18 @@ public CollectionModel<EntityModel<Employee>> search(@RequestParam(value = "name
110114
public EntityModel<Employee> findOne(@PathVariable Integer id) {
111115

112116
// Start the affordance with the "self" link, i.e. this method.
113-
Link findOneLink = linkTo(methodOn(WebMvcEmployeeController.class).findOne(id)).withSelfRel();
117+
WebMvcEmployeeController controller = methodOn(WebMvcEmployeeController.class);
118+
119+
Link findOneLink = linkTo(controller.findOne(id)).withSelfRel();
114120

115121
// Define final link as means to find entire collection.
116-
Link employeesLink = linkTo(methodOn(WebMvcEmployeeController.class).all()).withRel("employees");
122+
Link employeesLink = linkTo(controller.all()).withRel("employees");
117123

118124
// Return the affordance + a link back to the entire collection resource.
119125
return new EntityModel<>(EMPLOYEES.get(id), //
120126
findOneLink //
121-
.andAffordance(afford(methodOn(WebMvcEmployeeController.class).updateEmployee(null, id))) // //
122-
.andAffordance(afford(methodOn(WebMvcEmployeeController.class).partiallyUpdateEmployee(null, id))), //
127+
.andAffordance(afford(controller.updateEmployee(null, id))) // //
128+
.andAffordance(afford(controller.partiallyUpdateEmployee(null, id))), //
123129
employeesLink);
124130
}
125131

@@ -148,7 +154,8 @@ public ResponseEntity<?> updateEmployee(@RequestBody EntityModel<Employee> emplo
148154
}
149155

150156
@PatchMapping("/employees/{id}")
151-
public ResponseEntity<?> partiallyUpdateEmployee(@RequestBody EntityModel<Employee> employee, @PathVariable Integer id) {
157+
public ResponseEntity<?> partiallyUpdateEmployee(@RequestBody EntityModel<Employee> employee,
158+
@PathVariable Integer id) {
152159

153160
Employee oldEmployee = EMPLOYEES.get(id);
154161
Employee newEmployee = oldEmployee;

0 commit comments

Comments
 (0)