在示例应用程序上从Play 2.4升级到Play 2.5后,当我运行时./activator clean dist
,我没有遇到任何错误,但是在转到特定端点后http://localhost:9000/java/proxy
,我收到以下错误:
[error] application - ! @72keog237 - Internal server error, for (GET) [/java/proxy] -> play.api.http.HttpErrorHandlerExceptions$$anon$1: Execution exception[[CompletionException: java.lang.RuntimeException: There is no HTTP Context available from here.]] at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:293) at play.api.http.DefaultHttpErrorHandler.onServerError(HttpErrorHandler.scala:220) at play.api.GlobalSettings$class.onError(GlobalSettings.scala:160) at play.api.DefaultGlobal$.onError(GlobalSettings.scala:188) at play.api.http.GlobalSettingsHttpErrorHandler.onServerError(HttpErrorHandler.scala:100) at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:100) at play.core.server.netty.PlayRequestHandler$$anonfun$2$$anonfun$apply$1.applyOrElse(PlayRequestHandler.scala:99) at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:344) at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:343) at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:32) Caused by: java.util.concurrent.CompletionException: java.lang.RuntimeException: There is no HTTP Context available from here. at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:273) at java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:280) at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:604) at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577) ... 5 common frames omitted Caused by: java.lang.RuntimeException: There is no HTTP Context available from here. at play.mvc.Http$Context.current(Http.java:62) at play.mvc.Controller.response(Controller.java:81) at controllers.JavaController$4.apply(JavaController.java:107) at controllers.JavaController$4.apply(JavaController.java:103) at java.util.concurrent.CompletableFuture.uniApply(CompletableFuture.java:602) at java.util.concurrent.CompletableFuture$UniApply.tryFire(CompletableFuture.java:577) at java.util.concurrent.CompletableFuture$Completion.exec(CompletableFuture.java:443) at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
我一直在使用Replaced F.Promise中的指南和Java 8的CompletionStage替换F.Promise,map和flatMap以及上一个链接的建议替换(下面反映的更改片段).
public CompletionStageproxy() { final CompletionStage responsePromise = WS.url("http://example.com").get(); Logger.info("Before map"); final CompletionStage resultPromise = responsePromise.thenApplyAsync( new Function () { @Override public Result apply(WSResponse wsResponse) { Logger.info("Within map"); response().setContentType(wsResponse.getHeader("Content-Type")); return ok(wsResponse.getBody()); } } );
回到2.4时,我没有收到此错误,http://localhost:9000/java/proxy
端点也能成功运行.
在github上proxy
使用此公共示例应用程序替换方法中的相应替换调用后,我不断收到上述no HTTP Context available
错误.我也在Java8Controller类中获得了与http://localhost:9000/java8/proxy
端点相同的结果.
我找到了其他遇到过这个问题的人,到目前为止,我已经能够猜测我需要一个HttpExecutionContext调用和/或使用supplyAsync,尽管我在将这些知识转移到这个例子时遇到了麻烦.也许有人可以举一两个(或三个)关于我如何解决这个错误的例子?欢迎任何建议,并提前感谢您.
根据Play文档,在Action中使用Java CompletionStage时,必须显式提供HTTP执行上下文作为执行程序.因此,您可以在Action中注入HTTP Context.
public class Application extends Controller { @Inject HttpExecutionContext ec; public CompletionStageindex() { someCompletableFuture.supplyAsync(() -> { // do something with request() }, ec.current()); } }
希望有所帮助.