gpt4 book ai didi

scala - 未找到隐式 FromRequestUnmarsharell

转载 作者:行者123 更新时间:2023-12-01 03:49:53 25 4
gpt4 key购买 nike

我正在尝试使用我的域对象作为请求/响应正文参数。我正在使用喷雾路由和 as[T]解码对象。但我不断收到 could not find implicit value for parameter um: spray.httpx.unmarshalling.FromRequestUnmarshaller .尽管我已经手动向伴随对象添加了隐式解码器,但我遇到了同样的错误。我不知道出了什么问题。我编写的这个用于事件的 JSON 序列化器/反序列化器是因为我需要序列化 ​​joda DateTime 对象。

package services

import spray.routing.HttpService
import org.joda.time.DateTime
import org.joda.time.format.{DateTimeFormatter, ISODateTimeFormat}
import spray.httpx.unmarshalling.FromRequestUnmarshaller
import spray.json._
import services.EventsService.Event
import spray.httpx.SprayJsonSupport

trait EventsService extends HttpService {

val eventsRoute =
path("/events") {
get {
import EventsService._
entity(as[Event]) { event =>
complete(s"${event.toString}")
}
}
}
}

object EventsService extends DefaultJsonProtocol with SprayJsonSupport{
trait DateFormatter {
val formatter: DateTimeFormatter
}

trait DateParser {
val parser: DateTimeFormatter
}

implicit object EventFormatter extends RootJsonFormat[Event] with DateFormatter with DateParser {
def read(json: JsValue): Event = json match {
case obj: JsObject =>
val name = obj.fields.get("name").map(_.asInstanceOf[JsString].value).
getOrElse(deserializationError("field name not present"))
val city = obj.fields.get("city").map(_.asInstanceOf[JsString].value).
getOrElse(deserializationError("field city not present"))
val starts = obj.fields.get("starts").map(x => parser.parseDateTime(x.asInstanceOf[JsString].value)).
getOrElse(deserializationError("field starts not present"))
val ends = obj.fields.get("ends").map(x => parser.parseDateTime(x.asInstanceOf[JsString].value)).
getOrElse(deserializationError("ends field not present"))
Event(name, city, starts, ends)
case _ => deserializationError("wrong object to deserialize")
}

def write(e: Event): JsValue =
JsObject(Map(
"name" -> JsString(e.name),
"city" -> JsString(e.city),
"starts" -> JsString(formatter.print(e.starts)),
"ends" -> JsString(formatter.print(e.ends))
))

val formatter = ISODateTimeFormat.dateTimeNoMillis()
val parser = ISODateTimeFormat.dateTimeNoMillis().withOffsetParsed()
}

case class Event(name: String, city: String, starts: DateTime, ends: DateTime)
}

最佳答案

好的,我已经想通了。结构的顺序是错误的。首先应该是伴生对象,第二个特征是路由。我不知道为什么,但它现在有效。

关于scala - 未找到隐式 FromRequestUnmarsharell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23725435/

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