在示例应用上从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 with Java 8's CompletionStage中的指南替换F.Promise,map和flatMap以及上一个链接中建议的替换(下面反映的更改片段)。
public CompletionStage<Result> proxy() {
final CompletionStage<WSResponse> responsePromise = WS.url("http://example.com").get();
Logger.info("Before map");
final CompletionStage<Result> resultPromise = responsePromise.thenApplyAsync(
new Function<WSResponse, Result>() {
@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
端点成功运行。
使用此public sample app on github替换proxy
方法中的相应替换调用后,我不断收到上述no HTTP Context available
错误。我还在Java8Controller class中的http://localhost:9000/java8/proxy
端点获得了相同的结果。
我发现其他人遇到了这个问题,到目前为止,我已经能够猜测我需要HttpExecutionContext来电和/或使用supplyAsync,尽管我已经无法将这些知识转移到这个例子。也许有人可以举一两个(或三个)关于我如何解决这个错误的例子?欢迎任何建议,并提前感谢您。
答案 0 :(得分:10)
根据Play Documentation,在Action中使用Java CompletionStage时,必须显式提供HTTP执行上下文作为执行程序。 因此,您可以在Action中注入HTTP Context。
public class Application extends Controller {
@Inject HttpExecutionContext ec;
public CompletionStage<Result> index() {
someCompletableFuture.supplyAsync(() -> {
// do something with request()
}, ec.current());
}
}
希望有所帮助。