Spring Data REST Events Not working [duplicate]

2020-07-28 12:16发布

问题:

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.