SpringDataRest @RepositoryEventHandler not running when Controller is added

不羁岁月 提交于 2019-12-12 11:34:09

问题


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:


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.



来源:https://stackoverflow.com/questions/34210903/springdatarest-repositoryeventhandler-not-running-when-controller-is-added

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!