27
27
import java .util .stream .Collectors ;
28
28
import java .util .stream .IntStream ;
29
29
30
+ import org .springframework .hateoas .CollectionModel ;
31
+ import org .springframework .hateoas .EntityModel ;
30
32
import org .springframework .hateoas .IanaLinkRelations ;
31
33
import org .springframework .hateoas .Link ;
32
- import org .springframework .hateoas .EntityModel ;
33
- import org .springframework .hateoas .CollectionModel ;
34
34
import org .springframework .http .ResponseEntity ;
35
35
import org .springframework .web .bind .annotation .GetMapping ;
36
36
import org .springframework .web .bind .annotation .PatchMapping ;
@@ -61,9 +61,11 @@ public static void reset() {
61
61
public CollectionModel <EntityModel <Employee >> all () {
62
62
63
63
// 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 )));
67
69
68
70
// Return the collection of employee resources along with the composite affordance
69
71
return IntStream .range (0 , EMPLOYEES .size ()) //
@@ -97,10 +99,12 @@ public CollectionModel<EntityModel<Employee>> search(@RequestParam(value = "name
97
99
}
98
100
99
101
// 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 ()) //
101
105
.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 )));
104
108
105
109
// Return the collection of employee resources along with the composite affordance
106
110
return new CollectionModel <>(employees , selfLink );
@@ -110,16 +114,18 @@ public CollectionModel<EntityModel<Employee>> search(@RequestParam(value = "name
110
114
public EntityModel <Employee > findOne (@ PathVariable Integer id ) {
111
115
112
116
// 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 ();
114
120
115
121
// 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" );
117
123
118
124
// Return the affordance + a link back to the entire collection resource.
119
125
return new EntityModel <>(EMPLOYEES .get (id ), //
120
126
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 ))), //
123
129
employeesLink );
124
130
}
125
131
@@ -148,7 +154,8 @@ public ResponseEntity<?> updateEmployee(@RequestBody EntityModel<Employee> emplo
148
154
}
149
155
150
156
@ 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 ) {
152
159
153
160
Employee oldEmployee = EMPLOYEES .get (id );
154
161
Employee newEmployee = oldEmployee ;
0 commit comments