我在啓動應用程序中使用Spring Hateoas以避免手動創建視圖中的鏈接。它在Thymeleaf視圖中很好用,當控制器調用服務發送Thymeleaf也呈現的電子郵件時,它可以工作。如何使用Spring Hatoas ControllerLinkBuilder進行Thymeleaf模板化計劃電子郵件
創建鏈接的代碼是非常標準的
this.readLink = linkTo(methodOn(PostController.class)
.readPost(eventId, postId))
.withRel("ReadPost");
但對於一個@Scheduled
服務生成的電子郵件,它不能像這樣
015-08-23 22:28:40.886 ERROR 1180 --- [pool-2-thread-1] o.s.s.s.TaskUtils$LoggingErrorHandler : Unexpected error occurred in scheduled task.
java.lang.IllegalStateException: Could not find current request via RequestContextHolder. Is this being called from a Spring MVC handler?
at org.springframework.util.Assert.state(Assert.java:392) ~[spring-core-4.2.0.RELEASE.jar:4.2.0.RELEASE]
at org.springframework.hateoas.mvc.ControllerLinkBuilder.getCurrentRequest(ControllerLinkBuilder.java:242) ~[spring-hateoas-0.18.0.RELEASE.jar:na]
at org.springframework.hateoas.mvc.ControllerLinkBuilder.getBuilder(ControllerLinkBuilder.java:189) ~[spring-hateoas-0.18.0.RELEASE.jar:na]
at org.springframework.hateoas.mvc.ControllerLinkBuilderFactory.linkTo(ControllerLinkBuilderFactory.java:121) ~[spring-hateoas-0.18.0.RELEASE.jar:na]
有什麼我可以做的就是身邊缺少的由於代碼作爲@Scheduled
作業運行,因此有HttpServletRequest
?
這是我試圖在本週提出的Spring Data團隊中遇到的一個主要突出問題。現在沒有實際的解決方法。 – chrylis
好的,謝謝,不僅僅是我,那是 – Romell
你有沒有從Spring Data Team得到任何東西? – Romell