Spring Data REST Events Not working [duplicate]

痞子三分冷 提交于 2019-12-21 23:19:51

问题


I have tried to configure spring data rest event as per follows.All classes are in package org.springbootjpa

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

Following is my code

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

Event Handler

@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

@Entity
public class UserGroup {

    @Id
    @GeneratedValue
    private Long groupId;

    @Column
    private String groupName;
..
}

When I post an entry to the userGroups link the listener is not getting triggered.

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

回答1:


As mentioned in the comments the HandleBeforeCreate should be called in the case of POST request. The HandleBeforeSave event will be fired on PUT request.



来源:https://stackoverflow.com/questions/31130710/spring-data-rest-events-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!