From 7fe04d4000cf86b5dbd7ca581a3e77cdda32c649 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Mon, 4 Mar 2019 11:46:00 +0100 Subject: [PATCH] #833 - Cleanups in sample controllers for Spring WebMVC and WebFlux. --- .../support/WebFluxEmployeeController.java | 39 +++++++++---------- .../support/WebMvcEmployeeController.java | 33 +++++++++------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/test/java/org/springframework/hateoas/support/WebFluxEmployeeController.java b/src/test/java/org/springframework/hateoas/support/WebFluxEmployeeController.java index 487eba55..fb82edd7 100644 --- a/src/test/java/org/springframework/hateoas/support/WebFluxEmployeeController.java +++ b/src/test/java/org/springframework/hateoas/support/WebFluxEmployeeController.java @@ -27,11 +27,11 @@ import java.util.Optional; import java.util.TreeMap; import org.springframework.hateoas.Affordance; +import org.springframework.hateoas.CollectionModel; +import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.IanaLinkRelations; import org.springframework.hateoas.Link; import org.springframework.hateoas.Links; -import org.springframework.hateoas.EntityModel; -import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.server.reactive.WebFluxLinkBuilder; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; @@ -65,14 +65,14 @@ public class WebFluxEmployeeController { @GetMapping("/employees") public Mono>> all() { - Class controller = WebFluxEmployeeController.class; + WebFluxEmployeeController controller = methodOn(WebFluxEmployeeController.class); return Flux.fromIterable(EMPLOYEES.keySet()) // .flatMap(id -> findOne(id)) // .collectList() // - .flatMap(resources -> linkTo(methodOn(controller).all()).withSelfRel() // - .andAffordance(methodOn(controller).newEmployee(null)) // - .andAffordance(methodOn(controller).search(null, null)) // + .flatMap(resources -> linkTo(controller.all()).withSelfRel() // + .andAffordance(controller.newEmployee(null)) // + .andAffordance(controller.search(null, null)) // .toMono() // .map(selfLink -> new CollectionModel<>(resources, selfLink))); } @@ -82,6 +82,8 @@ public class WebFluxEmployeeController { @RequestParam Optional name, // @RequestParam Optional role) { + Class controller = WebFluxEmployeeController.class; + return Flux.fromIterable(EMPLOYEES.keySet()) // .flatMap(id -> findOne(id)) // .filter(resource -> { @@ -94,27 +96,24 @@ public class WebFluxEmployeeController { .orElse(true); return nameMatches && roleMatches; - }).collectList().flatMap(resources -> { - - Class controller = WebFluxEmployeeController.class; - - return linkTo(methodOn(controller).all()).withSelfRel() // - .andAffordance(methodOn(controller).newEmployee(null)) // - .andAffordance(methodOn(controller).search(null, null)) // - .toMono() // - .map(selfLink -> new CollectionModel<>(resources, selfLink)); - }); + }).collectList().flatMap(resources -> linkTo(methodOn(controller).all()).withSelfRel() // + .andAffordance(methodOn(controller).newEmployee(null)) // + .andAffordance(methodOn(controller).search(null, null)) // + .toMono() // + .map(selfLink -> new CollectionModel<>(resources, selfLink))); } @GetMapping("/employees/{id}") public Mono> findOne(@PathVariable Integer id) { - Mono selfLink = linkTo(methodOn(WebFluxEmployeeController.class).findOne(id)).withSelfRel() // - .andAffordance(methodOn(WebFluxEmployeeController.class).updateEmployee(null, id)) // - .andAffordance(methodOn(WebFluxEmployeeController.class).partiallyUpdateEmployee(null, id)) // + WebFluxEmployeeController controller = methodOn(WebFluxEmployeeController.class); + + Mono selfLink = linkTo(controller.findOne(id)).withSelfRel() // + .andAffordance(controller.updateEmployee(null, id)) // + .andAffordance(controller.partiallyUpdateEmployee(null, id)) // .toMono(); - Mono employeesLink = linkTo(methodOn(WebFluxEmployeeController.class).all()).withRel("employees") // + Mono employeesLink = linkTo(controller.all()).withRel("employees") // .toMono(); return selfLink.zipWith(employeesLink) // diff --git a/src/test/java/org/springframework/hateoas/support/WebMvcEmployeeController.java b/src/test/java/org/springframework/hateoas/support/WebMvcEmployeeController.java index 570b221e..2ece71d6 100644 --- a/src/test/java/org/springframework/hateoas/support/WebMvcEmployeeController.java +++ b/src/test/java/org/springframework/hateoas/support/WebMvcEmployeeController.java @@ -27,10 +27,10 @@ import java.util.TreeMap; import java.util.stream.Collectors; import java.util.stream.IntStream; +import org.springframework.hateoas.CollectionModel; +import org.springframework.hateoas.EntityModel; import org.springframework.hateoas.IanaLinkRelations; import org.springframework.hateoas.Link; -import org.springframework.hateoas.EntityModel; -import org.springframework.hateoas.CollectionModel; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PatchMapping; @@ -61,9 +61,11 @@ public class WebMvcEmployeeController { public CollectionModel> all() { // Generate an "Affordance" based on this method (the "self" link) - Link selfLink = linkTo(methodOn(WebMvcEmployeeController.class).all()).withSelfRel() // - .andAffordance(afford(methodOn(WebMvcEmployeeController.class).newEmployee(null))) // - .andAffordance(afford(methodOn(WebMvcEmployeeController.class).search(null, null))); + WebMvcEmployeeController controller = methodOn(WebMvcEmployeeController.class); + + Link selfLink = linkTo(controller.all()).withSelfRel() // + .andAffordance(afford(controller.newEmployee(null))) // + .andAffordance(afford(controller.search(null, null))); // Return the collection of employee resources along with the composite affordance return IntStream.range(0, EMPLOYEES.size()) // @@ -97,10 +99,12 @@ public class WebMvcEmployeeController { } // Generate an "Affordance" based on this method (the "self" link) - Link selfLink = linkTo(methodOn(WebMvcEmployeeController.class).all()) // + WebMvcEmployeeController controller = methodOn(WebMvcEmployeeController.class); + + Link selfLink = linkTo(controller.all()) // .withSelfRel() // - .andAffordance(afford(methodOn(WebMvcEmployeeController.class).newEmployee(null))) // - .andAffordance(afford(methodOn(WebMvcEmployeeController.class).search(null, null))); + .andAffordance(afford(controller.newEmployee(null))) // + .andAffordance(afford(controller.search(null, null))); // Return the collection of employee resources along with the composite affordance return new CollectionModel<>(employees, selfLink); @@ -110,16 +114,18 @@ public class WebMvcEmployeeController { public EntityModel findOne(@PathVariable Integer id) { // Start the affordance with the "self" link, i.e. this method. - Link findOneLink = linkTo(methodOn(WebMvcEmployeeController.class).findOne(id)).withSelfRel(); + WebMvcEmployeeController controller = methodOn(WebMvcEmployeeController.class); + + Link findOneLink = linkTo(controller.findOne(id)).withSelfRel(); // Define final link as means to find entire collection. - Link employeesLink = linkTo(methodOn(WebMvcEmployeeController.class).all()).withRel("employees"); + Link employeesLink = linkTo(controller.all()).withRel("employees"); // Return the affordance + a link back to the entire collection resource. return new EntityModel<>(EMPLOYEES.get(id), // findOneLink // - .andAffordance(afford(methodOn(WebMvcEmployeeController.class).updateEmployee(null, id))) // // - .andAffordance(afford(methodOn(WebMvcEmployeeController.class).partiallyUpdateEmployee(null, id))), // + .andAffordance(afford(controller.updateEmployee(null, id))) // // + .andAffordance(afford(controller.partiallyUpdateEmployee(null, id))), // employeesLink); } @@ -148,7 +154,8 @@ public class WebMvcEmployeeController { } @PatchMapping("/employees/{id}") - public ResponseEntity partiallyUpdateEmployee(@RequestBody EntityModel employee, @PathVariable Integer id) { + public ResponseEntity partiallyUpdateEmployee(@RequestBody EntityModel employee, + @PathVariable Integer id) { Employee oldEmployee = EMPLOYEES.get(id); Employee newEmployee = oldEmployee;