5

I have an event handler that runs perfectly fine on a repository. However once I add a controller into the mix and call the repository method directly, the EventHandler seems to be skipped over.

Has anyone encountered this "issue"? If so, what can I do to get the event handler to start running again?

1 Answer 1

8

So you expect that your event handler is called when you use a custom controller. I think this expectation is false. The event handler is just called when spring data rests RepositoryEntityController is in control. It is not an entity event listener on JPA level.

What you could do is call the event handler manually. The spring-data-rest RepositoryEventHandler is a using normal spring application events. So your controller could implement ApplicationEventPublisherAware and publish one of the spring-data-rest application events. These are all subclasses of org.springframework.data.rest.core.event.RepositoryEvent

applicationEventPublisher.publishEvent(new AfterCreateEvent(myEntity)); 

See the spring documentation for details.

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

3 Comments

Based on the available documentation, I don't think it's unreasonable for a new user to make this assumption if their controller is annotated with @RepositoryRestController. It is, afterall, called @RepositoryEventHandler, not @RepositoryControllerEventHandler
@Snekse that might be the case - I can just tell you that it is not the case - but I detailed my answer a bit with information on how you could still make use of the RepositoryEventHandler in your custom controller.
As Mathias said, you can publish your own events. Make sure you're handling them all (Before, After, Create, Save, List, Delete). Here is another good example. stackoverflow.com/a/37616850/378151 In our project we have a MyEntityValidator that we call from the controller and the Repo event handler also calls.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.