Play 2.5升级错误:CompletionException - 此处没有可用的HTTP上下文 [英] Play 2.5 upgrade error: CompletionException - There is no HTTP Context available from here

查看:349
本文介绍了Play 2.5升级错误:CompletionException - 此处没有可用的HTTP上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在示例应用程序上从Play 2.4升级到Play 2.5后,当我运行 ./ activator clean dist 时,我在进入特定端点后没有出现任何错误 http:// localhost:9000 / java / proxy ,我收到以下错误:

After upgrading from Play 2.4 to Play 2.5 on a sample app, when I run ./activator clean dist, I get no errors however after going to a specific endpoint http://localhost:9000/java/proxy, I'm receiving the following error:

[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)

我一直在使用将F.Promise替换为Java 8的CompletionStage ,将F.Promise,map和flatMap替换为上一个链接的建议替换(反映下面的更改片段)。

I have been using the guide from the Replaced F.Promise with Java 8's CompletionStage replacing F.Promise, map, and flatMap with the suggested replacements from the previous link (reflected snippet of changes below).

  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 端点成功运行。

When going back to 2.4, I don't get this error and the http://localhost:9000/java/proxy endpoint works successfully.

使用此代理方法中的相应替换调用后.com / btgrant-76 / Play-2-Java-Scala-Java-8-Async-Comparison / blob / 6a85cf31cfb804ef20bacf8e14d30ce46cc9307c / app / controllers / JavaController.java#L86-L99rel =nofollow noreferrer>公共示例应用程序github ,我一直得到没有HTTP上下文可用错误。我也从 http:// localhost:9000 / java8 / proxy 端点获得相同的结果/Play-2-Java-Scala-Java-8-Async-Comparison/blob/6a85cf31cfb804ef20bacf8e14d30ce46cc9307c/app/controllers/Java8Controller.java#L71-L83\"rel =nofollow noreferrer> Java8Controller类

After replacing the appropriate replacement calls in the proxy method using this public sample app on github, I keep getting the no HTTP Context available error above. I also get the same result with http://localhost:9000/java8/proxy endpoint in the Java8Controller class.

我发现其他人遇到过这个问题,到目前为止我已经猜到我需要一个 HttpExecutionContext 调用和/或使用 supplyAsync 虽然我在将这些知识转移到这个例子时遇到了麻烦。也许有人可以举一两个(或三个)关于我如何解决这个错误的例子?欢迎任何建议,并提前感谢您。

I found others who have come across this problem and so far I've been able to conjecture that I need a HttpExecutionContext call and/or use supplyAsync although I've been having trouble transferring that knowledge to this example. Perhaps someone can give an example or two (or three) on how I may resolve this error? Any suggestions welcome, and thank you in advance.

推荐答案

根据播放文档,在Action中使用Java CompletionStage时,必须显式提供HTTP执行上下文作为执行者。
所以你可以在你的Action中注入HTTP Context。

As per the Play Documentation, you must supply the HTTP execution context explicitly as an executor when using a Java CompletionStage inside an Action. So you can inject HTTP Context in your Action.

public class Application extends Controller {
@Inject HttpExecutionContext ec;

public CompletionStage<Result> index() {
    someCompletableFuture.supplyAsync(() -> { 
      // do something with request()
    }, ec.current());
  }
}

希望有所帮助。

这篇关于Play 2.5升级错误:CompletionException - 此处没有可用的HTTP上下文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆