Spring Data Rest:未调用RepositoryEventHandler方法 [英] Spring Data Rest: RepositoryEventHandler methods not invoked

查看:455
本文介绍了Spring Data Rest:未调用RepositoryEventHandler方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加RepositoryEventHandler,如 Spring数据库REST文档到REST存储库,如下所示:

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
}

我创建了一个AgentEventHandler:

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());

    }
}

并在@Configuration中声明它组件:

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();
    }
}

当我发布到REST资源时,实体得到持久化但方法handleBeforeSave永远不会被调用。我错过了什么?

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

我正在使用:Spring Boot 1.1.5.RELEASE

I'm using: Spring Boot 1.1.5.RELEASE

推荐答案

有时候明显的错误会被忽视。

Sometimes obvious mistakes go unnoticed.

POST - 一个Spring Data REST资源,发出 BeforeCreateEvent 。要捕获此事件,方法handleBeforeSave必须使用@HandleBeforeCreate而不是@HandleBeforeSave进行注释(后者在PUT和PATCH HTTP调用时调用)。

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.

这篇关于Spring Data Rest:未调用RepositoryEventHandler方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆