Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 将JSON发布到Akka Http中的端点_Scala_Akka Http - Fatal编程技术网

Scala 将JSON发布到Akka Http中的端点

Scala 将JSON发布到Akka Http中的端点,scala,akka-http,Scala,Akka Http,我正在尝试创建HttpRequest并尝试向端点提交json,我的rest服务是使用akka http编写的 case class JobInfo(jobName: String, appId: Option[String] = None, status: Option[String] = None, modify_date: Option[Long] = None) obje

我正在尝试创建HttpRequest并尝试向端点提交json,我的rest服务是使用akka http编写的

  case class JobInfo(jobName: String,
                   appId: Option[String] = None,
                   status: Option[String] = None,
                   modify_date: Option[Long] = None)

object Test extends App{

import HttpMethods._
  val fieldList = List(
    ("jobName",Json.fromString("TestJobA")),
    ("appId",Json.fromString("1234")),
    ("status",Json.fromString("Running")),
    ("modify_date",Json.fromLong(DateTime.now.getMillis))
  )
  val json = Json.fromFields(fieldList)

  val endPoint = Uri.from(scheme = "http",
    host = "0.0.0.0",
    port = 7000,
    path = s"/updateJobDetails/").toString()

  val requestEntity = HttpEntity(MediaTypes.`application/json`, json.toString)
//  val postRequest1 = HttpRequest.POST("/receive").withEntity(requestEntity)
  HttpRequest(POST,"http://0.0.0.0:7000/updateJobDetails",entity = requestEntity)

} 
但这似乎不起作用, 早些时候,我用喷雾剂来做同样的事情

  val pipe: HttpRequest => Future[HttpResponse] = sendReceive
          val json =
            s"""{
               |  "jobName" : "jobA",
               |  "appId" :"${jobInfo.appId.getOrElse("Not Set")}",
               |  "status" :"${jobInfo.status.getOrElse("Not Set")}",
               |  "modify_date" :${DateTime.now.getMillis}
               |}""".stripMargin.parseJson.asJsObject()

          //Rest EndPoint for JobDetails updation.
          val endPoint = Uri.from(scheme = "http",
            host ="0.0.0.0",
            port = 7000,
            path = s"/updateJobDetails").toString()

          val response = pipe(Put(endPoint, json))
当你说“不工作”时,有人能帮我纠正我所缺少的吗?同时提供你收到的错误信息

不管怎样,根据你上面发布的代码。你正在建设一个未来,但你不是在等待它的完成

下面的代码显示了如何构建未来,然后等待它完成

object Client {
  def main(args: Array[String]): Unit = {
    implicit val system = ActorSystem()
    implicit val materializer = ActorMaterializer()
    // needed for the future flatMap/onComplete in the end
    implicit val executionContext = system.dispatcher

    val responseFuture: Future[HttpResponse] = Http().singleRequest(HttpRequest(uri = "http://akka.io"))

    responseFuture
      .onComplete {
        case Success(res) => println(res)
        case Failure(_)   => sys.error("something wrong")
      }
    Await.result(responseFuture, Duration.Inf)
  }
}