Spring Data Rest: RepositoryEventHandler methods not invoked

10,972

Solution 1

Sometimes obvious mistakes go unnoticed.

POST-ing a Spring Data REST resource, emits a BeforeCreateEvent. To catch this event, the method handleBeforeSave must be annotated with @HandleBeforeCreate instead of @HandleBeforeSave (the latter gets invoked on PUT and PATCH HTTP calls).

Tests pass successfully on my (cleaned up) demo app now.

Solution 2

How does your main Application class look like? Does it import the RepositoryRestMvcConfiguration as described in https://spring.io/guides/gs/accessing-data-rest/?

Share:
10,972
dimi
Author by

dimi

Updated on June 07, 2022

Comments

  • dimi
    dimi almost 2 years

    I am trying to add a RepositoryEventHandler as described on Spring Data REST documentation to the REST repository shown below:

    @RepositoryRestResource(collectionResourceRel = "agents", path = "/agents")
    public interface AgentRepository extends CrudRepository<Agent, Long> {
        // no implementation required; Spring Data will create a concrete Repository
    }
    

    I created an AgentEventHandler:

    @Component
    @RepositoryEventHandler(Agent.class)
    public class AgentEventHandler {
    
        /**
         * Called before {@link Agent} is persisted 
         * 
         * @param agent
         */
        @HandleBeforeSave
        public void handleBeforeSave(Agent agent) {
    
            System.out.println("Saving Agent " + agent.toString());
    
        }
    }
    

    and declared it in a @Configuration component:

    @Configuration
    public class RepositoryConfiguration {
    
        /**
         * Declare an instance of the {@link AgentEventHandler}
         *
         * @return
         */
        @Bean
        AgentEventHandler agentEvenHandler() {
    
            return new AgentEventHandler();
        }
    }
    

    When I am POSTing to the REST resource, the Entity gets persisted but the method handleBeforeSave never gets invoked. What am I missing?

    I'm using: Spring Boot 1.1.5.RELEASE

  • dimi
    dimi over 9 years
    Marcel: I pushed an example project on github. Appreciate your help.
  • Beamie
    Beamie almost 7 years
    Before I start doing a little matrix myself over which events are emitted on specific HTTP calls, is this documented somewhere? I had a look in the Spring Data REST documentation, but it only mentions the events, not in detail to which HTTP call they will be emitted.
  • dimi
    dimi almost 7 years
    @Beamie I did a quick search and couldn't find any documentation linking Spring Data REST events with HTTP verbs. The following should work tho: POST=>*CreateEvent, PUT=>*SaveEvent, DELETE=>*DeleteEvent. Detailed list of events: docs.spring.io/spring-data/rest/docs/current/reference/html/‌​…
  • Vinícius M
    Vinícius M almost 4 years
    Note for future readers: the Events (BeforeCreate, BeforeSave, etc) are not called if you override the default controller methods, so keep that in mind.