Java Spring数据REST事件不工作
我已经尝试按照以下方式配置spring数据rest事件。所有类都在包Java Spring数据REST事件不工作,java,spring,spring-boot,spring-data-rest,Java,Spring,Spring Boot,Spring Data Rest,我已经尝试按照以下方式配置spring数据rest事件。所有类都在包org.springbootjpa 活动: 下面是我的代码 @SpringBootApplication public class DemoApplication { public static void main(String[] args) { ApplicationContext context = SpringApplication.run( DemoApplic
org.springbootjpa
活动:
下面是我的代码
@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();
}
}
事件处理程序
@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
public class UserGroup {
@Id
@GeneratedValue
private Long groupId;
@Column
private String groupName;
..
}
当我将条目发布到userGroups链接时,侦听器不会被触发
post --data "{groupId:1,groupName:'group1'}"
如注释中所述,在POST请求的情况下,应调用
把手。HandleBeforeSave
事件将在PUT请求时触发。您的DemoApplication
所在的包和DemoConfig
的包是什么。建议不要使用DemoConfig
,只需将@Bean
方法添加到DemoApplication
。它们在同一个包中。。然而,我已经按照你的建议改变了。。还是一样的结果你没听错吗?POST将创建一个条目,我怀疑调用的是@handlebeforerecreate
,而不是save,save用于更新,即PUT。我认为POST请求只触发创建事件(“@handlebeforerecreate”),请使用PUT请求尝试您的事件。是的。。就这样。。我听错了。。车把对我有用。。