不调用Spring数据休息@HandleBeforeCreate方法

时间:2016-10-11 07:21:20

标签: spring rest spring-boot spring-data-rest

我有2节课。 PostComment。帖子@HandleBeforeCreate运行正常,但评论@HandleBeforeCreate没有。我想知道为什么?

PostEventHandler类:

@Component
@RepositoryEventHandler(Post.class)
public class PostEventHandler {

    @HandleBeforeCreate
    public void setPostAuthorname(Post Post) {
        System.out.println("This method called successfully!");
    }
}

PostRepository接口:

@RepositoryRestResource(collectionResourceRel = "posts", path = "posts")
public interface PostRepository extends MongoRepository<Post, String> {

}

Post类没有自定义的Controller / Resource实现。但在我的注释存储库界面中,我有一个自定义方法,它看起来像这样:

@RepositoryRestResource(collectionResourceRel = "comments", path = "comments")
public interface CommentRepository extends MongoRepository<Comment, String> {
    // announceId is a field in Comment class. Get method works fine
    List<Comment> findAllByAnnounceId(String announceId);
}

CoomentEventHandler类:

@Component
@RepositoryEventHandler(Comment.class)
public class CommentEventHandler {

    @HandleBeforeCreate
    public void setCommentAuthorUsername(Comment comment) {
        System.out.println("This method never gets invoked!");
    }
}

自定义CommentController实现:

@RepositoryRestController
public class CommentController {

    @Autowired
    private AnnounceRepository announceRepository;

    @Autowired
    private CommentRepository commentRepository;

    @RequestMapping(value = "/announces/{announceId}/comments", method = RequestMethod.GET)
    public ResponseEntity<List<Comment>> getAllComments(@PathVariable("announceId") String announceId) {
        System.out.println("This method called successfully with a valid PathVariable!);
        // Custom interface method works fine
        List<Comment> comments = commentRepository.findAllByAnnounceId(announceId);
        if (comments != null) {
            System.out.println("This method called successfully!);
            return new ResponseEntity<>(comments, HttpStatus.OK);
        }
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }   

    @RequestMapping(value = "/announces/{announceId}/comments", method = RequestMethod.POST)
    public ResponseEntity<Comment> createComment(@PathVariable("announceId") String announceId, @RequestBody Comment comment) {
        System.out.println("This method called successfully with a valid PathVariable and Comment object!");
        Announce announce = announceRepository.findOne(announceId);
        if (announce != null) {
            commentRepository.save(comment);
            announce.getCommentList().add(comment);
            announceRepository.save(announce);
            return new ResponseEntity<>(HttpStatus.CREATED);
        }
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
}

1 个答案:

答案 0 :(得分:2)

只有在公开的Spring Data REST端点上触发HTTP请求时,才会调用事件处理程序(如@HandleBeforeCreate带注释的方法)。这就是POST请求on / posts路径触发setPostAuthorname方法的原因。 在CommentController的情况下,您公开了自定义请求映射方法,并直接调用方法存储库,这样,永远不会触发事件处理程序。您使用此方法的唯一方法是在CommentController中注入事件处理程序Bean并调用相应的方法,并调用save repository方法的调用,或者直接从用户界面调用,我的意思是,从客户端调用,并执行来自UI的createComment方法的逻辑,通过公开/评论API路径执行POST请求。

问候。