Spring Data REST事件不起作用

编程入门 行业动态 更新时间:2024-10-27 20:39:38
本文介绍了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事件不起作用

本文发布于:2023-11-22 17:24:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   事件   Spring   Data   REST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!