【问题标题】:Spring Data Rest @HandleBeforeCreate method is not called未调用 Spring Data Rest @HandleBeforeCreate 方法
【发布时间】:2017-02-19 16:45:04
【问题描述】:

我有 2 节课。 Post 和 Comment。发布@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 类没有自定义控制器/资源实现。但是在我的 Comments Repository 界面中,我有一个自定义方法,它看起来像这样:

@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);
    }
}

【问题讨论】:

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


    【解决方案1】:

    仅当您在公开的 Spring Data REST 端点上触发 HTTP 请求时才会调用事件处理程序(如 @HandleBeforeCreate 注释方法)。这就是 /posts 路径上的 POST 请求触发 setPostAuthorname 方法的原因。 在 CommentController 的情况下,您会暴露自定义请求映射方法,并直接调用方法存储库,这样就不会触发事件处理程序。您使用这种方法的唯一方法是在您的 CommentController 中注入事件处理程序 Bean,并在调用保存存储库方法之前和之后调用相应的方法,或者直接从用户界面调用,我的意思是,从客户端调用,然后执行来自 UI 的 createComment 方法的逻辑,通过暴露的 /cmets API 路径执行 POST 请求。

    问候。

    【讨论】:

      猜你喜欢
      • 2014-10-20
      • 2016-01-08
      • 2020-01-15
      • 1970-01-01
      • 2014-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-14
      相关资源
      最近更新 更多