gpt4 book ai didi

scala - 一次带有文件上传extractRequestContext和案例类的Akka HTTP API服务?

转载 作者:行者123 更新时间:2023-12-05 01:26:42 24 4
gpt4 key购买 nike

如何编写Akka HTTP 服务,带文件上传和case class 来接受前端的输入请求?我有两个服务,例如

     val upload1 = path("api" / "upload1") {
extractClientIP {
ip =>
optionalHeaderValueByName(Constants.AUTH) {
auths =>
(post & extractRequestContext) { request =>
// functionality
}
}
}
}

val upload2= path("api" / "upload2") {
extractClientIP {
ip =>
optionalHeaderValueByName(Constants.AUTH) {
auths =>
(post & entity(as[Create])) { create =>
// functionality
}
}
}
}

我需要将上述两个 API 合并到一个请求调用中,而不是两个请求调用,我该如何实现这一点,我已经尝试过如下但它不起作用

 val upload1 = path("api" / "upload1") {
extractClientIP {
ip =>
optionalHeaderValueByName(Constants.AUTH) {
auths =>
(post & extractRequestContext & entity(as[Create]) { request =>
// functionality
}
}
}
}

我遇到了缺少参数类型的问题

我在使用时遇到问题

(post & extractRequestContext & entity(as[String])) { (requestContext, create) =>
// do stuff
complete("ok")
}

ERROR akka.actor.ActorSystemImpl - Error during processing of request: 'Substream Source cannot be materialized more than once'. Completing with 500 Internal Server Error response. To change default exception handling behavior, provide a custom ExceptionHandler.
java.lang.IllegalStateException: Substream Source cannot be materialized more than once
at akka.stream.impl.fusing.SubSource$$anon$4.setCB(StreamOfStreams.scala:725) ~[akka-stream_2.11-2.4.19.jar:na]
at akka.stream.impl.fusing.SubSource$$anon$4.preStart(StreamOfStreams.scala:735) ~[akka-stream_2.11-2.4.19.jar:na]
at akka.stream.impl.fusing.GraphInterpreter.init(GraphInterpreter.scala:520) ~[akka-stream_2.11-2.4.19.jar:na]
at akka.stream.impl.fusing.GraphInterpreterShell.init(ActorGraphInterpreter.scala:380) ~[akka-stream_2.11-2.4.19.jar:na]
at akka.stream.impl.fusing.ActorGraphInterpreter.tryInit(ActorGraphInterpreter.scala:538) ~[akka-stream_2.11-2.4.19.jar:na]
at akka.stream.impl.fusing.ActorGraphInterpreter.preStart(ActorGraphInterpreter.scala:586) ~[akka-stream_2.11-2.4.19.jar:na]
at akka.actor.Actor$class.aroundPreStart(Actor.scala:510) ~[akka-actor_2.11-2.4.19.jar:na]
at akka.stream.impl.fusing.ActorGraphInterpreter.aroundPreStart(ActorGraphInterpreter.scala:529) ~[akka-stream_2.11-2.4.19.jar:na]
at akka.actor.ActorCell.create(ActorCell.scala:590) ~[akka-actor_2.11-2.4.19.jar:na]
at akka.actor.ActorCell.invokeAll$1(ActorCell.scala:461) ~[akka-actor_2.11-2.4.19.jar:na]
at akka.actor.ActorCell.systemInvoke(ActorCell.scala:483) ~[akka-actor_2.11-2.4.19.jar:na]
at akka.dispatch.Mailbox.processAllSystemMessages(Mailbox.scala:282) [akka-actor_2.11-2.4.19.jar:na]
at akka.dispatch.Mailbox.run(Mailbox.scala:223) [akka-actor_2.11-2.4.19.jar:na]
at akka.dispatch.Mailbox.exec(Mailbox.scala:234) [akka-actor_2.11-2.4.19.jar:na]
at scala.concurrent.forkjoin.ForkJoinTask.doExec(ForkJoinTask.java:260) [scala-library-2.11.11.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1339) [scala-library-2.11.11.jar:na]
at scala.concurrent.forkjoin.ForkJoinPool.runWorker(ForkJoinPool.java:1979) [scala-library-2.11.11.jar:na]
at scala.concurrent.forkjoin.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:107) [scala-library-2.11.11.jar:na]

This is my post man request

最佳答案

如果您使用 & 运算符组合两个指令,您将得到一个元组,其中包含从所有指令 到完成您的路线。

在您的情况下,您连接了 2 个指令,每个指令提取 1 个值(extractRequestContextentity(as[Create])),以及一个不提取参数的指令( 发布)。因此,您需要从 2 个参数(RequestContextCreate)向您的路由提供一个函数。

  val upload1 = path("api" / "upload1") {
extractClientIP {
ip =>
optionalHeaderValueByName("tmp") {
auths =>
(post & extractRequestContext & entity(as[String])) { (requestContext, create) =>
// do stuff
complete("ok")
}
}
}
}

关于scala - 一次带有文件上传extractRequestContext和案例类的Akka HTTP API服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48879791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com