mdm*_*mdm 2 scala intellij-idea spray
我已将Spray Client的示例代码复制到我自己的项目中,以便轻松使用它.我使用IntelliJ 13.
这是我的代码:
package mypackage
import scala.util.Success
import scala.concurrent.duration._
import akka.actor.ActorSystem
import akka.pattern.ask
import akka.event.Logging
import akka.io.IO
import spray.json.{JsonFormat, DefaultJsonProtocol}
import spray.can.Http
import spray.util._
import spray.client.pipelining._
import scala.util.Success
import scala.util.Failure
case class Elevation(location: Location, elevation: Double)
case class Location(lat: Double, lng: Double)
case class GoogleApiResult[T](status: String, results: List[T])
object ElevationJsonProtocol extends DefaultJsonProtocol {
implicit val locationFormat = jsonFormat2(Location)
implicit val elevationFormat = jsonFormat2(Elevation)
implicit def googleApiResultFormat[T :JsonFormat] = jsonFormat2(GoogleApiResult.apply[T])
}
object SprayExample extends App {
// we need an ActorSystem to host our application in
implicit val system = ActorSystem("simple-spray-client")
import system.dispatcher // execution context for futures below
val log = Logging(system, getClass)
log.info("Requesting the elevation of Mt. Everest from Googles Elevation API...")
val pipeline = sendReceive ~> unmarshal[GoogleApiResult[Elevation]]
val responseFuture = pipeline {
Get("http://maps.googleapis.com/maps/api/elevation/json?locations=27.988056,86.925278&sensor=false")
}
responseFuture onComplete {
case Success(GoogleApiResult(_, Elevation(_, elevation) :: _)) =>
log.info("The elevation of Mt. Everest is: {} m", elevation)
shutdown()
case Success(somethingUnexpected) =>
log.warning("The Google API call was successful but returned something unexpected: '{}'.", somethingUnexpected)
shutdown()
case Failure(error) =>
log.error(error, "Couldn't get elevation")
shutdown()
}
def shutdown(): Unit = {
IO(Http).ask(Http.CloseAll)(1.second).await
system.shutdown()
}
}
Run Code Online (Sandbox Code Playgroud)
就目前而言,这完美无瑕,并按预期印刷了Mt.Everest的高度.
如果我将文件在包结构中向下移动一级,就会发生奇怪的事情,即创建一个mypackage.myinnerpackage并在其中移动文件.
IDEA改变了我的第一行代码package mypackage.myinnerpackage,就是这样.
然后我尝试运行应用程序,编译将失败,并显示以下消息:
could not find implicit value for evidence parameter of type spray.httpx.unmarshalling.FromResponseUnmarshaller[courserahelper.sprayexamples.GoogleApiResult[courserahelper.sprayexamples.Elevation]]
val pipeline = sendReceive ~> unmarshal[GoogleApiResult[Elevation]]
^
Run Code Online (Sandbox Code Playgroud)
我没有更改代码中的任何内容,我实际上只是更改了包!另外,这段代码是自包含的,它不依赖于我在代码的任何其他部分声明的任何其他隐式....
我错过了什么?
谢谢!
(用这个支持正确格式化的答案替换了注释.)
在您使用之前,您发布的代码缺少这两个导入unmarshal:
import ElevationJsonProtocol._
import SprayJsonSupport._
val pipeline = sendReceive ~> unmarshal[GoogleApiResult[Elevation]]
Run Code Online (Sandbox Code Playgroud)
它存在于原始代码中.IntelliJ有时会搞乱进口,这可能是他们在转型中迷失的原因.
| 归档时间: |
|
| 查看次数: |
1947 次 |
| 最近记录: |