【问题标题】:Spring Data REST Events Not working [duplicate]Spring Data REST事件不起作用[重复]
【发布时间】:2015-06-30 06:03:12
【问题描述】:

我已尝试按如下方式配置spring数据休息事件。所有类都在包org.springbootjpa

事件:http://docs.spring.io/spring-data/rest/docs/current/reference/html/#events

以下是我的代码

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(
                DemoApplication.class, args);
        String[] beanNames = context.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }

    }

    @Bean
    GroupEventHandler groupEventHandler() {
        return new GroupEventHandler();
    }
}

事件处理程序

@RepositoryEventHandler(UserGroup.class)
public class GroupEventHandler {

    @HandleBeforeSave
    public void handleGroupSave(UserGroup group) {
        System.out.println("Inside handleGroupSave ....");
    }

    @HandleAfterSave
    public void handleAfterSave(UserGroup group) {
        System.out.println("Inside handleAfterSave ....");
    }

}

实体

@Entity
public class UserGroup {

    @Id
    @GeneratedValue
    private Long groupId;

    @Column
    private String groupName;
..
}

当我向 userGroups 链接发布条目时,监听器没有被触发。

post --data "{groupId:1,groupName:'group1'}"

【问题讨论】:

  • 你的DemoApplication所在的包和DemoConfig的包是什么。建议不要使用DemoConfig,只需将@Bean 方法添加到DemoApplication
  • 它们在同一个包中..但是我已经按照你的建议进行了更改..仍然是相同的结果
  • 你不是在监听错误的事件吗? POST 将创建一个条目,我怀疑调用了 @HandleBeforeCreate 而不是保存,这是用于更新,即 PUT。
  • 我认为在 POST 请求中只会触发创建事件('@HandleBeforeCreate'),请使用 PUT 请求尝试您的事件。
  • 是的 .. 就是这样 .. 我听错了事件 .. HandleBeforeCreate 对我有用 ..

标签: java spring spring-boot spring-data-rest


【解决方案1】:

如 cmets 中所述,应在 POST 请求的情况下调用 HandleBeforeCreateHandleBeforeSave 事件将在 PUT 请求时触发。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-07
    • 1970-01-01
    • 2019-01-14
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 2016-06-05
    • 2012-06-01
    相关资源
    最近更新 更多