akka 使用HttpClient从Java API调用Scala服务不起作用

polhcujo  于 2022-11-05  发布在  Java
关注(0)|答案(1)|浏览(141)

我正在尝试使用Java 11中的HttpClient从Java API调用Scala服务。
在Java API上,代码如下所示:

var client = HttpClient.newHttpClient();
var packagesCountUrl = getPackagesCountUrl();
var sessionUserToken = sessionUserHelper.getSessionUserToken();

var request = HttpRequest.newBuilder(URI.create(packagesCountUrl))
    .GET()
    .header(HttpHeaders.AUTHORIZATION, sessionUserToken)
    .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
    .timeout(Duration.of(10, SECONDS))
    .build();

var response = client.send(request, HttpResponse.BodyHandlers.ofString());

在Java API上,我收到了一个带有503状态代码的响应,在Scala API的日志中有以下内容:

ERROR] [10/05/2021 18:59:29.711] [abc-api-actorSystem-akka.actor.default-dispatcher-11] [akka://abc-api-actorSystem/system/StreamSupervisor-0/flow-24966-1-mapAsyncUnordered] Error in stage [ExposeAttributes(akka.http.impl.engine.http2.Http2Blueprint$$$Lambda$889/2094115865@e6b910)]: requirement failed: Requests with method 'CONNECT' must have an empty entity
java.lang.IllegalArgumentException: requirement failed: Requests with method 'CONNECT' must have an empty entity
    at scala.Predef$.require(Predef.scala:277)
    at akka.http.scaladsl.model.HttpRequest.<init>(HttpMessage.scala:276)
    at akka.http.scaladsl.model.HttpRequest$.apply(HttpMessage.scala:442)
    at akka.http.impl.engine.http2.RequestParsing$.rec$1(RequestParsing.scala:84)
    at akka.http.impl.engine.http2.RequestParsing$.$anonfun$parseRequest$3(RequestParsing.scala:140)
    at akka.http.impl.util.ExposeAttributes$$anon$14.onPush(StreamUtils.scala:354)
    at akka.stream.impl.fusing.GraphInterpreter.processPush(GraphInterpreter.scala:523)
    at akka.stream.impl.fusing.GraphInterpreter.execute(GraphInterpreter.scala:409)
    at akka.stream.impl.fusing.GraphInterpreterShell.runBatch(ActorGraphInterpreter.scala:606)
    at akka.stream.impl.fusing.ActorGraphInterpreter$SimpleBoundaryEvent.execute(ActorGraphInterpreter.scala:47)
    at akka.stream.impl.fusing.ActorGraphInterpreter$SimpleBoundaryEvent.execute$(ActorGraphInterpreter.scala:43)
    at akka.stream.impl.fusing.ActorGraphInterpreter$BatchingActorInputBoundary$OnNext.execute(ActorGraphInterpreter.scala:85)
    at akka.stream.impl.fusing.GraphInterpreterShell.processEvent(ActorGraphInterpreter.scala:581)
    at akka.stream.impl.fusing.ActorGraphInterpreter.akka$stream$impl$fusing$ActorGraphInterpreter$$processEvent(ActorGraphInterpreter.scala:749)
    at akka.stream.impl.fusing.ActorGraphInterpreter$$anonfun$receive$1.applyOrElse(ActorGraphInterpreter.scala:764)
    at akka.actor.Actor.aroundReceive(Actor.scala:539)
    at akka.actor.Actor.aroundReceive$(Actor.scala:537)
    at akka.stream.impl.fusing.ActorGraphInterpreter.aroundReceive(ActorGraphInterpreter.scala:671)
    at akka.actor.ActorCell.receiveMessage(ActorCell.scala:612)
    at akka.actor.ActorCell.invoke(ActorCell.scala:581)
    at akka.dispatch.Mailbox.processMailbox(Mailbox.scala:268)
    at akka.dispatch.Mailbox.run(Mailbox.scala:229)
    at akka.dispatch.Mailbox.exec(Mailbox.scala:241)
    at akka.dispatch.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260)
    at akka.dispatch.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339)
    at akka.dispatch.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979)
    at akka.dispatch.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107)
vxqlmq5t

vxqlmq5t1#

问题是Scala API及其配置不支持HTTP2。
解决方案是让HttpClient改用HTTP1。

var client = HttpClient.newHttpClient();
var packagesCountUrl = getPackagesCountUrl(loopLabel);
var sessionUserToken = sessionUserHelper.getSessionUserToken();

var request = HttpRequest.newBuilder(URI.create(packagesCountUrl))
        .GET()
        .header(HttpHeaders.AUTHORIZATION, sessionUserToken)
        .header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
        .version(HttpClient.Version.HTTP_1_1) // This was the SOLUTION
        .timeout(Duration.of(10, SECONDS))
        .build();

var response = client.send(request, HttpResponse.BodyHandlers.ofString());

相关问题