17

I am trying to add a RepositoryEventHandler as described on Spring Data REST documentation to the REST repository shown below:

@RepositoryRestResource(collectionResourceRel = "agents", path = "/agents") public interface AgentRepository extends CrudRepository<Agent, Long> { // no implementation required; Spring Data will create a concrete Repository } 

I created an AgentEventHandler:

@Component @RepositoryEventHandler(Agent.class) public class AgentEventHandler { /** * Called before {@link Agent} is persisted * * @param agent */ @HandleBeforeSave public void handleBeforeSave(Agent agent) { System.out.println("Saving Agent " + agent.toString()); } } 

and declared it in a @Configuration component:

@Configuration public class RepositoryConfiguration { /** * Declare an instance of the {@link AgentEventHandler} * * @return */ @Bean AgentEventHandler agentEvenHandler() { return new AgentEventHandler(); } } 

When I am POSTing to the REST resource, the Entity gets persisted but the method handleBeforeSave never gets invoked. What am I missing?

I'm using: Spring Boot 1.1.5.RELEASE

10
  • Does it work if you rather extend AbstractRepositoryEventListener? Commented Aug 26, 2014 at 16:28
  • @OliverGierke (thanks for stepping in) no that made no difference [public class AgentEventHandler extends AbstractRepositoryEventListener] nor [public class AgentEventHandler extends AbstractRepositoryEventListener<Agent>] Commented Aug 26, 2014 at 16:34
  • Are you sure the RepositoryConfiguration class is picked up by Boot? If so, any chance you create a tiny failing sample project and create a ticket in our JIRA? Commented Aug 26, 2014 at 16:37
  • @OliverGierke I think it is (I added a System.out.println("registering AgentEventHandler"); inside the agentEvenHandler method...if there are better ways to check this please let me know). Jira: I'll give it a shot! Commented Aug 26, 2014 at 16:49
  • @OliverGierke Not sure if it's really a bug or something I've misconfigured..So I pushed an example project on github instead of creating a ticket. Commented Aug 26, 2014 at 21:00

2 Answers 2

30

Sometimes obvious mistakes go unnoticed.

POST-ing a Spring Data REST resource, emits a BeforeCreateEvent. To catch this event, the method handleBeforeSave must be annotated with @HandleBeforeCreate instead of @HandleBeforeSave (the latter gets invoked on PUT and PATCH HTTP calls).

Tests pass successfully on my (cleaned up) demo app now.

Sign up to request clarification or add additional context in comments.

3 Comments

Before I start doing a little matrix myself over which events are emitted on specific HTTP calls, is this documented somewhere? I had a look in the Spring Data REST documentation, but it only mentions the events, not in detail to which HTTP call they will be emitted.
@Beamie I did a quick search and couldn't find any documentation linking Spring Data REST events with HTTP verbs. The following should work tho: POST=>*CreateEvent, PUT=>*SaveEvent, DELETE=>*DeleteEvent. Detailed list of events: docs.spring.io/spring-data/rest/docs/current/reference/html/…
Note for future readers: the Events (BeforeCreate, BeforeSave, etc) are not called if you override the default controller methods, so keep that in mind.
1

How does your main Application class look like? Does it import the RepositoryRestMvcConfiguration as described in https://spring.io/guides/gs/accessing-data-rest/?

1 Comment

Marcel: I pushed an example project on github. Appreciate your help.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.