Scala 使用akka http反序列化LocalDateTime
我试图在akka http中添加对序列化和反序列化LocalDateTime的支持。我对以下错误感到有点困惑: 错误:(42,20)找不到参数um的隐式值: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[SomeData] 实体(作为[SomeData]){evt=>complete(StatusCodes.Created)} 错误:(42,20)没有足够的参数用于方法as:(隐式um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[SomeData])akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[SomeData]。 未指定值参数um。 实体(作为[SomeData]){evt=>complete(StatusCodes.Created)} 我在下面的代码中缺少了什么?我将代码粘贴到Akka http文档中的极简示例中。我正在运行Akka http 10.0.6Scala 使用akka http反序列化LocalDateTime,scala,akka-http,Scala,Akka Http,我试图在akka http中添加对序列化和反序列化LocalDateTime的支持。我对以下错误感到有点困惑: 错误:(42,20)找不到参数um的隐式值: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[SomeData] 实体(作为[SomeData]){evt=>complete(StatusCodes.Created)} 错误:(42,20)没有足够的参数用于方法as:(隐式um: akka.http.scaladsl.u
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.model._
import akka.http.scaladsl.server.Directives._
import akka.stream.ActorMaterializer
import scala.io.StdIn
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import spray.json.{DefaultJsonProtocol, JsString, JsValue, JsonFormat}
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import akka.http.scaladsl.server.Route
final case class SomeData (dateTime: LocalDateTime)
trait JsonSupport extends SprayJsonSupport with DefaultJsonProtocol {
implicit val eventDataFormat: JsonFormat[SomeData] = jsonFormat1(SomeData)
implicit val localDateTimeFormat = new JsonFormat[LocalDateTime] {
private val iso_date_time = DateTimeFormatter.ISO_DATE_TIME
def write(x: LocalDateTime) = JsString(iso_date_time.format(x))
def read(value: JsValue) = value match {
case JsString(x) => LocalDateTime.parse(x, iso_date_time)
case x => throw new RuntimeException(s"Unexpected type ${x.getClass.getName} when trying to parse LocalDateTime")
}
}
// implicit val eventDataFormat: JsonFormat[SomeData] = jsonFormat1(SomeData) ** moved here **
}
object Main extends JsonSupport {
def main(args: Array[String]): Unit = {
implicit val system = ActorSystem("my-system")
implicit val materializer = ActorMaterializer()
implicit val executionContext = system.dispatcher
val route:Route =
path("hello") {
post {
entity(as[SomeData]) { evt => complete(StatusCodes.Created) }
}
}
val bindingFuture = Http().bindAndHandle(route, "localhost", 8080)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
bindingFuture
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
}
↓
↓
implicit val eventDataFormat: JsonFormat[SomeData] = jsonFormat1(SomeData)
implicit val eventDataFormat = jsonFormat1(SomeData)