Spring Data Rest에서 EventHandler 동작안할때(별도URL 요청시)

2018. 7. 18. 18:10IT개발/Spring Boot & Spring Data Rest & JPA

반응형

@HandleBeforeCreate 와 같은 EventHandler는 Spring Data Rest에서 제공하는 기본 HTTP 요청(POST/ PATCH/ PUT/ DELETE 등)일때만 동작한다.

즉 Customize하게 Controller에서 별도 HTTP 요청(POST/ PATCH/ PUT/ DELETE 등)을 만들경우, 동작되지 않으므로

트랜잭션처리하는 서비스 레이어에 해당 EventHandler 를 재활용하여 동작하도록 하면 되겠다.


아래는 참고자료 되시겠다.

https://stackoverflow.com/questions/39972215/spring-data-rest-handlebeforecreate-method-is-not-called



Event Handlers (like @HandleBeforeCreate annotated methods) are only invoked when you fires a HTTP Request on exposed Spring Data REST endpoints. 

That's the reason why the POST request on /posts path triggers the setPostAuthorname method. 

In the CommentController case, you are exposed custom request mapping methods, and invoking directly the method repositories, at this way, never will be triggered the event handlers. 

The only way that you hav using this approach is injecting the Event Handler Bean in your CommentController and invoke corresponding methods before and afeter the invocation of save repository method, OR, invoking directly from the User Interface, 

I mean, from the client, and perform the logic of createComment method from UI, doing a POST request over the exposed /comments API path.

Regards.

반응형