Spring REST data events do not work

I tried to configure the spring data wait event as follows. All classes are in the package.org.springbootjpa

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

Below 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 ....");
    }

}

An object

@Entity
public class UserGroup {

    @Id
    @GeneratedValue
    private Long groupId;

    @Column
    private String groupName;
..
}

When I post an entry to the userGroups link, the listener does not start.

post --data "{groupId:1,groupName:'group1'}"
+1
source share
1 answer

As mentioned in the comments, HandleBeforeCreateshould be called in case of a POST request. The event HandleBeforeSavewill be fired at the request of PUT.

+2
source

Source: https://habr.com/ru/post/1679428/


All Articles