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'}" 
5
  • 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. Commented Jun 30, 2015 at 8:49
  • They were in the same package .. However I have changed as you have suggested .. Still the same result Commented Jun 30, 2015 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. Commented Jun 30, 2015 at 10:43
  • 1
    I think on POST requests only create events are fired ('@HandleBeforeCreate'), try your events with PUT request. Commented Jun 30, 2015 at 11:06
  • Yes .. That's it .. I was listening to the wrong event .. HandleBeforeCreate works for me .. Commented Jul 1, 2015 at 5:32

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.