Event Handling

Posted By : Preeti Singh | 28-Dec-2020

Introduction: This article is all about handling events in Spring Data Rest and we will talk about how you can handle the events when Spring data rest perform operation and Spring data Rest generates specific events that you can handle to write a different business logic before the persistence.

There are eight different events that the REST exporter releases throughout the process of working with an entity:

* BeforeCreateEvent

* AfterCreateEvent

* BeforeSaveEvent

* AfterSaveEvent

* BeforeLinkSaveEvent

* AfterLinkSaveEvent

* BeforeDeleteEvent

* AfterDeleteEvent

I will talk about some specific ones in this blog.

1 Getting Started.

1.1 Adding dependency.

Gradle dependency

dependencies {






Maven dependency










Also Read: Spring Security along with OAuth2.0


For Spring-based projects

Maven Dependency







1.2 Creating a Domain.




public class User {


    @GeneratedValue(strategy = GenerationType.AUTO)

    private long id;

    private String firstName;

    private String lastName;




1.3 Creating a Data Rest Resource Repository

@RepositoryRestResource(collectionResourceRel = "/users", path = "users")

public interface UserRepository extends PagingAndSortingRepository<User, Long>{





Also Read: Spring Cloud Config Server

2. Implementing Spring Data Rest event listeners


There are two ways to implement Event listeners.
* You can extend your class using AbstractRepositoryEventListener to override the methods that you want to catch before persistence.
* Annotation-based Handlers.


2.1 Implementing Rest event listeners by extending class AbstractRepositoryEventListeners


public class AbstractEventListeners extends AbstractRepositoryEventListener{


    protected void onBeforeCreate(Object entity) {
        System.out.println("This gets triggered before object creation.");

    protected void onAfterCreate(Object entity) {
        System.out.println("This gets triggered before object creation.");






2.1 Using the annotation-based config.




public class DataRestResourceEventListener {



    public void handlePersitanceBeforeSave(Object entity) {

        //Your Business logic here



    public void handlePersitanceAfterSave(Object entity) {
        //Your Business logic here






Note: You can also specify the specific type event handlers bypassing the domain type inside handler annotation i.e. @RepositoryEventHandler(User.class)

That's all guys for this article and you can feel free to drop your comments here and feedback if you have any. 


We, at Oodles Technologies, provide full-scale SaaS app development services to build scalable, responsive, and quality-driven web and mobile applications. Contact us for technical assistance or drop us a line at info@oodlestechnologies.com for more detail.

About Author

Author Image
Preeti Singh

Preeti Singh is a backend developer and has experience in developing web applications using java, j2EE, Spring Framework. In mean time she loves listening to music.

Request for Proposal

Name is required

Comment is required

Sending message..