From 04e0dcf4d045231599ca030b4673f1ba22a618a2 Mon Sep 17 00:00:00 2001 From: Greg Turnquist Date: Tue, 5 Dec 2017 17:20:27 -0600 Subject: [PATCH] Polishing --- affordances/README.adoc | 42 +++-------------------------------------- 1 file changed, 3 insertions(+), 39 deletions(-) diff --git a/affordances/README.adoc b/affordances/README.adoc index e5b962d..1c3a9be 100644 --- a/affordances/README.adoc +++ b/affordances/README.adoc @@ -112,10 +112,8 @@ class EmployeeResourceAssembler extends SimpleIdentifiableResourceAssembler resource) { resource.getContent().getId() - .ifPresent(id -> { - resource.add(getCollectionLinkBuilder().slash(resource.getContent()).withSelfRel() - .andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, id)))); - }); + .ifPresent(id -> resource.add(getCollectionLinkBuilder().slash(resource.getContent()).withSelfRel() + .andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, id))))); resource.add(getCollectionLinkBuilder().withRel(this.getRelProvider().getCollectionResourceRelFor(this.getResourceType()))); } @@ -440,42 +438,8 @@ This augments the same REST controller with a *GET* operation for an individual to update/edit. Take your team to read both flows. The key part you must define, is the corresponding `EmployeeResourceAssembler.toResource(Employee)` method. -In the heart of that method, is its `addLinks(Resource resource)` method: -[source,java] ----- -@Component -class EmployeeResourceAssembler extends SimpleIdentifiableResourceAssembler { - - ... - - /** - * Define links to add to every {@link Resource}. - * - * @param resource - */ - @Override - protected void addLinks(Resource resource) { - - resource.getContent().getId() - .ifPresent(id -> resource.add(getCollectionLinkBuilder().slash(resource.getContent()).withSelfRel() - .andAffordance(afford(methodOn(EmployeeController.class).updateEmployee(null, id))))); - - resource.add(getCollectionLinkBuilder().withRel(this.getRelProvider().getCollectionResourceRelFor(this.getResourceType()))); - } - - ... -} ----- - -In this situation, you are taking an individual `Resource`, extracting the contents (i.e. the `Employee` itself), grabbing -its `Optional` *id*, and if present, creating a *self* link to it. Again, because `Employee` implements `Identifiable`, `slash(Employee)` -is able to extract the *id* field to form a proper link. This defines the *GET* endpoint as a Spring HATEOAS `Link`. - -Using this *self* link, you then add an affordance to the controller's `updateEmployee` method. Spring HATEOAS's Affordances API -is able to inspect the Spring MVC annotations and extract information to render a HAL-FORMS endpoint. - -If you restart the application and ping `/employees/1`, you can see an individual entry: +If you ping `/employees/1`, you can see an individual entry: [source,javascript] ----