From e6de869935a1730fca251e4e93fc63ab37dbe39b Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Tue, 7 Aug 2012 11:02:23 -0500 Subject: [PATCH] Updated wiki docs about event handling for render events. --- doc/handling_events.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/doc/handling_events.md b/doc/handling_events.md index 22b5d22f3..e18691326 100644 --- a/doc/handling_events.md +++ b/doc/handling_events.md @@ -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 +### 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")); + } + + }