本文介绍了Spring Data REST事件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试按照以下方式配置spring数据休息事件。所有类都在包 org.springbootjpa
<事件: http://docs.spring。我/我的代码 @SpringBootApplication 公共类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(){返回新的GroupEventHandler(); } }事件处理程序
@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 公共类UserGroup { @Id @GeneratedValue private Long的groupId; @Column private String groupName; .. }当我将条目发布到userGroups链接时侦听器未被触发。
post --data{groupId:1,groupName:'group1'}解决方案
如评论中所述, HandleBeforeCreate 。将在PUT请求时触发 HandleBeforeSave 事件。
I have tried to configure spring data rest event as per follows.All classes are in package org.springbootjpa
Events: 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'}"解决方案
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.
更多推荐
Spring Data REST事件不起作用
发布评论