喷射 Http Post 请求

Spray Http Post request

这是我试图通过 spray http 创建 Post 请求的代码 包裹 org.exadatum.xstream.service.http

import akka.actor.ActorSystem
import akka.util.Timeout
import spray.http._
import spray.httpx.SprayJsonSupport
import spray.client.pipelining._
import spray.json._

import scala.concurrent.duration._
import scala.concurrent.{ExecutionContext, Future}

object Update extends App with SprayJsonSupport with AdditionalFormats{
   implicit val system: ActorSystem = ActorSystem("UpdateJob")
   implicit val executor: ExecutionContext = system.dispatcher
   implicit val timeout: Timeout = Timeout(1.seconds)

   val pipe: HttpRequest => Future[HttpResponse] = sendReceive

   val json = """{
                |  "jobName" : "land",
                |  "appId" : "application_12323121211223",
                |  "status" : "started",
                |  "runningSince" : 1498144649660
                |}""".parseJson.asJsObject()

   pipe(Post("http://localhost:3000/job/v1/updateJobDetails/land",json))
}

我的休息服务是 运行 localhost:3000。这样做会导致错误

Exception in thread "main" java.lang.NoSuchMethodError: scala.collection.immutable.HashSet$.empty()Lscala/collection/immutable/HashSet;
at akka.actor.ActorCell$.<init>(ActorCell.scala:336)
at akka.actor.ActorCell$.<clinit>(ActorCell.scala)
at akka.actor.RootActorPath.$div(ActorPath.scala:159)
at akka.actor.LocalActorRefProvider.<init>(ActorRefProvider.scala:464)
at akka.actor.LocalActorRefProvider.<init>(ActorRefProvider.scala:452)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at akka.actor.ReflectiveDynamicAccess$$anonfun$createInstanceFor.apply(DynamicAccess.scala:78)
at scala.util.Try$.apply(Try.scala:192)
at akka.actor.ReflectiveDynamicAccess.createInstanceFor(DynamicAccess.scala:73)
at akka.actor.ReflectiveDynamicAccess$$anonfun$createInstanceFor.apply(DynamicAccess.scala:84)
at akka.actor.ReflectiveDynamicAccess$$anonfun$createInstanceFor.apply(DynamicAccess.scala:84)
at scala.util.Success.flatMap(Try.scala:231)
at akka.actor.ReflectiveDynamicAccess.createInstanceFor(DynamicAccess.scala:84)
at akka.actor.ActorSystemImpl.liftedTree1(ActorSystem.scala:584)
at akka.actor.ActorSystemImpl.<init>(ActorSystem.scala:577)
at akka.actor.ActorSystem$.apply(ActorSystem.scala:141)
at akka.actor.ActorSystem$.apply(ActorSystem.scala:108)
at org.exadatum.xstream.service.http.Update$.delayedEndpoint$org$exadatum$xstream$service$http$Update(UpdateJob.scala:14)
at org.exadatum.xstream.service.http.Update$delayedInit$body.apply(UpdateJob.scala:13)
at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main.apply(App.scala:76)
at scala.App$$anonfun$main.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at com.job.service.http.Update$.main(UpdateJob.scala:13)
at com.job.service.http.Update.main(UpdateJob.scala)

我尝试了 post Sending post with json using spray?

中提到的解决方案

这很可能是 Scala 库不匹配(根据您引用 hashmap 的具体错误)。尝试使用 scala-library-2.10.jar(来自此处:(run-main-0) java.lang.NoSuchMethodError

此外,也许您需要正确导入 SprayJsonSupport? :

import spray.httpx.SprayJsonSupport._

注意尾随 ._