Java – spring data rest: the repositoryeventhandler method was not called
•
Java
I tried to add a repository EventHandler to the rest repository as follows:
@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 declare it in the @ configuration component:
@Configuration public class RepositoryConfiguration { /** * Declare an instance of the {@link AgentEventHandler} * * @return */ @Bean AgentEventHandler agentEvenHandler() { return new AgentEventHandler(); } }
When I publish to a rest resource, the entity is persisted, but the method handlebeforesave is never called I'm missing what
I use spring boot 1.1 5.RELEASE
Solution
Sometimes obvious mistakes are not ignored
When publishing a spring data rest resource, a beforecreateevent will be issued To capture this event, you must inject the method handlebeforesave (which is called in put and patch HTTP calls) using @ handlebeforcreate instead of @ handlebeforesave
The test successfully passed my demo app
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码