Rafael
Rafael

Reputation: 2676

Enrich behavior on spring-data-rest repository

I am using spring-data-rest repositories to expose usual CRUD operations from a REST API.

However i need to add additional functionality to those operations.

e.g. I want to send an HTTP request on delete to a third party API.

I was wondering how to override the behavior of some of the methods provided by JpaRepository (or any other data-rest repo)

Also not sure if this shall be implemented at repository level or providing a custom controller ... in that case i am concerned on how to disable access to repository endpoint so all operations pass through my code.

Upvotes: 0

Views: 489

Answers (1)

jny
jny

Reputation: 8057

spring-data-rest supports events.

You can either extend an AbstractRepositoryEventListener or a class annotated with @RepositoryEventHandler.

In your case it would be:

public class AfterDeleteEventListener extends AbstractRepositoryEventListener {


  @Override
  public void onAfterDelete(Object entity) {
    //your code here
  }
}

or

@RepositoryEventHandler (MyClass.class)
public class MyEventHandler {

  @HandleAfterDelete
  public void handleDelete(MyClass p) {
    // your code here
  }


}

Upvotes: 2

Related Questions