Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringDataRest @RepositoryEventHandler not running when Controller is added

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?

like image 327
kennyg Avatar asked Dec 10 '15 20:12

kennyg


1 Answers

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.

like image 159
Mathias Dpunkt Avatar answered Sep 28 '22 05:09

Mathias Dpunkt