dim*_*imi 16 java spring spring-data spring-data-rest
我正在尝试将Spring Data REST文档中描述的RepositoryEventHandler添加到下面显示的REST存储库:
@RepositoryRestResource(collectionResourceRel = "agents", path = "/agents")
public interface AgentRepository extends CrudRepository<Agent, Long> {
// no implementation required; Spring Data will create a concrete Repository
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个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());
}
}
Run Code Online (Sandbox Code Playgroud)
并在@Configuration组件中声明它:
@Configuration
public class RepositoryConfiguration {
/**
* Declare an instance of the {@link AgentEventHandler}
*
* @return
*/
@Bean
AgentEventHandler agentEvenHandler() {
return new AgentEventHandler();
}
}
Run Code Online (Sandbox Code Playgroud)
当我发布到REST资源时,实体会被持久化,但方法handleBeforeSave永远不会被调用.我错过了什么?
我正在使用:Spring Boot 1.1.5.RELEASE
dim*_*imi 23
有时候明显的错误会被忽视.
POST一个Spring Data REST资源,发出一个BeforeCreateEvent.要捕获此事件,必须使用@HandleBeforeCreate而不是@HandleBeforeSave(后者在PUT和PATCH HTTP调用上调用)来注释方法handleBeforeSave.
测试现在已成功通过我的(清理过的)演示应用程序.
归档时间: |
|
查看次数: |
9063 次 |
最近记录: |