Skip to content

Polish doc #419

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/main/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ The `Link` value object follows the Atom link definition and consists of a `rel`
----
Link link = new Link("http://localhost:8080/something");
assertThat(link.getHref(), is("http://localhost:8080/something"));
assertThat(link.getRel(), is(Link.SELF));
assertThat(link.getRel(), is(Link.REL_SELF));

Link link = new Link("http://localhost:8080/something", "my-rel");
assertThat(link.getHref(), is("http://localhost:8080/something"));
Expand Down Expand Up @@ -88,7 +88,7 @@ You can also easily access links contained in that resource:
----
Link selfLink = new Link("http://myhost/people");
assertThat(resource.getId(), is(selfLink));
assertThat(resource.getLink(Link.SELF), is(selfLink));
assertThat(resource.getLink(Link.REL_SELF), is(selfLink));
----
[[fundamentals.obtaining-links]]
=== Obtaining links
Expand Down Expand Up @@ -136,7 +136,7 @@ The `ControllerLinkBuilder` uses Spring's `ServletUriComponentsBuilder` under th
Person person = new Person(1L, "Dave", "Matthews");
// /person / 1
Link link = linkTo(PersonController.class).slash(person.getId()).withSelfRel();
assertThat(link.getRel(), is(Link.SELF));
assertThat(link.getRel(), is(Link.REL_SELF));
assertThat(link.getHref(), endsWith("/people/1"));
----

Expand Down Expand Up @@ -169,15 +169,15 @@ As of version 0.4 you can even easily build links pointing to methods or creatin
Method method = PersonController.class.getMethod("show", Long.class);
Link link = linkTo(method, 2L).withSelfRel();

assertThat(link.getHref(), is("/people/2")));
assertThat(link.getHref(), endsWith("/people/2")));
----

This is still a bit dissatisfying as we have to get a `Method` instance first, which throws an exception and is generally quite cumbersome. At least we don't repeat the mapping. An even better approach is to have a dummy method invocation of the target method on a controller proxy we can create easily using the `methodOn(…)` helper.

[source, java]
----
Link link = linkTo(methodOn(PersonController.class).show(2L)).withSelfRel();
assertThat(link.getHref(), is("/people/2")));
assertThat(link.getHref(), endsWith("/people/2")));
----

`methodOn(…)` creates a proxy of the controller class that is recording the method invocation and exposed it in a proxy created for the return type of the method. This allows the fluent expression of the method we want to obtain the mapping for. However there are a few constraints on the methods that can be obtained using this technique:
Expand Down Expand Up @@ -256,14 +256,14 @@ class PersonResourceAssembler extends ResourceAssemblerSupport<Person, PersonRes
@Override
public PersonResource toResource(Person person) {

PersonResource resource = createResource(person);
PersonResource resource = createResourceWithId(person);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is actually correct. But your other points are valid.

// … do further mapping
return resource;
}
}
----

Setting the class up like this gives you the following benefits: there are a hand full of `createResource(…)` methods that will allow you to create an instance of the resource and have it a `Link` with a rel of `self` added to it. The href of that link is determined by the configured controllers request mapping plus the id of the `Identifiable` (e.g. `/people/1` in our case). The resource type gets instantiated by reflection and expects a no-arg constructor. Simply override `instantiateResource(…)` in case you'd like to use a dedicated constructor or avoid the reflection performance overhead.
Setting the class up like this gives you the following benefits: there are a hand full of `createResourceWithId(…)` methods that will allow you to create an instance of the resource and have it a `Link` with a rel of `self` added to it. The href of that link is determined by the configured controllers request mapping plus the id of the `Identifiable` (e.g. `/people/1` in our case). The resource type gets instantiated by reflection and expects a no-arg constructor. Simply override `instantiateResource(…)` in case you'd like to use a dedicated constructor or avoid the reflection performance overhead.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See previous comment


The assembler can then be used to either assemble a single resource or an `Iterable` of them:

Expand Down