2

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'}"
ArunM
  • 2,274
  • 3
  • 25
  • 47
  • What is the package your `DemoApplication` is in and the package of the `DemoConfig`. Suggesting instead of using `DemoConfig` simply add the `@Bean` method to the `DemoApplication`. – M. Deinum Jun 30 '15 at 08:49
  • They were in the same package .. However I have changed as you have suggested .. Still the same result – ArunM Jun 30 '15 at 10:30
  • 1
    Aren't you listening for the wrong events? A POST will create an entry and I suspect that a `@HandleBeforeCreate` is called instead of save, which is for updates i..e PUT. – M. Deinum Jun 30 '15 at 10:43
  • 1
    I think on POST requests only create events are fired ('@HandleBeforeCreate'), try your events with PUT request. – Dominika Jun 30 '15 at 11:06
  • Yes .. That's it .. I was listening to the wrong event .. HandleBeforeCreate works for me .. – ArunM Jul 01 '15 at 05:32

1 Answers1

3

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.

ArunM
  • 2,274
  • 3
  • 25
  • 47