-
Notifications
You must be signed in to change notification settings - Fork 471
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
Closed
Polish doc #419
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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")); | ||
|
@@ -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 | ||
|
@@ -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")); | ||
---- | ||
|
||
|
@@ -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: | ||
|
@@ -256,14 +256,14 @@ class PersonResourceAssembler extends ResourceAssemblerSupport<Person, PersonRes | |
@Override | ||
public PersonResource toResource(Person person) { | ||
|
||
PersonResource resource = createResource(person); | ||
PersonResource resource = createResourceWithId(person); | ||
// … 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.