Updated wiki docs about event handling for render events.

This commit is contained in:
Jon Brisbin
2012-08-07 11:02:23 -05:00
parent 88fc8ba7cd
commit e6de869935

View File

@@ -1,6 +1,6 @@
# Handling ApplicationEvents in the REST Exporter
There are six different events that the REST exporter emits throughout the process of working with an entity. Those are:
There are eight different events that the REST exporter emits throughout the process of working with an entity. Those are:
* BeforeSaveEvent
* AfterSaveEvent
@@ -8,6 +8,8 @@ There are six different events that the REST exporter emits throughout the proce
* AfterLinkSaveEvent
* BeforeDeleteEvent
* AfterDeleteEvent
* BeforeRenderResourcesEvent
* BeforeRenderResourceEvent
### ApplicationListener
@@ -109,3 +111,27 @@ You can pass the base package of the packages you want searched for handlers in
<property name="basePackage" value="com.mycompany.repository.handlers"/>
</bean>
### Handling Render events
It's possible to alter the representation of the resource that gets sent back to the client. There are two possible reponses: a `org.springframework.data.rest.core.Resources` bean, which has a `content` and `links` property. The `content` property is a list of `org.springframework.data.rest.core.Resource` objects. In the case of JPA @Entities, these will actually be the `org.springframework.data.rest.core.MapResource` subclass of `Resource`. These helpers are what are run through the JSON mapper and used to produce the output sent to the client.
If, for example, you want to add links to the representation for a `Person`, you could define a POJO class and annotate it with `@RepositoryEventHandler(Person.class)` just like in the above example. Then you could define a couple of methods for altering the representations of the `Resources` and `Resource` objects that are sent to the client.
The `org.springframework.data.rest.core.Resources` bean is sent to the client whenever lists or results are required. If you call a query method or ask for a list of entities, then the `Resources` response is the one you'll get. If you ask for a specific entity's representation, however, you'll want to alter the plain `org.springframework.data.rest.core.Resource` bean.
An example handler class for these would look something like this:
@RepositoryEventHandler(Person.class)
public class PersonRenderHandler {
@HandleBeforeRenderResources
public void handleBeforeRenderResources(ServerHttpRequest request, RepositoryMetadata repoMeta, Resources resources) {
resources.addLink(new SimpleLink("linkAddedByHandler", new URI("http://localhost:8080/linkAddedByHandler"));
}
@HandleBeforeRenderResource
public void handleBeforeRenderResource(ServerHttpRequest request, RepositoryMetadata repoMeta, Resource resource) {
resource.addLink(new SimpleLink("linkAddedByHandler", new URI("http://localhost:8080/linkAddedByHandler"));
}
}