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 {
...
compile("org.springframework.boot:spring-boot-starter-data-rest")
...
}
Maven dependency
--------------------
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
...
</dependencies>
Also Read: Spring Security along with OAuth2.0
For Spring-based projects
Maven Dependency
--------------------
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>3.3.4.RELEASE</version>
</dependency>
1.2 Creating a Domain.
---------------------
@Entity @Data @NoArgsConstructor public class User { @Id @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
--------------------------------------------------------------------------------------------
@Component public class AbstractEventListeners extends AbstractRepositoryEventListener{
@Override
protected void onBeforeCreate(Object entity) {
System.out.println("This gets triggered before object creation.");
}@Override
protected void onAfterCreate(Object entity) {
System.out.println("This gets triggered before object creation.");
}}
-------------------------------------------------------------
2.1 Using the annotation-based config.
-------------------------------------------------------------
@RepositoryEventHandler @Component public class DataRestResourceEventListener { @HandleBeforeSave public void handlePersitanceBeforeSave(Object entity) { //Your Business logic here }
@HandleAfterSave
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 [email protected] for more detail.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.