gpt4 book ai didi

scala - Akka -http : could not find implicit value for parameter unmarshalling

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

我的喷雾 json 支持看起来像这样

object MarshallingSupport extends SprayJsonSupport {
implicit def json4sFormats: Formats = DefaultFormats
}

在我的 route ,我想将请求映射到 dto
object Main extends App with AppConfig with BaseService with MainActorSystem {

val processor = system.actorOf(Props(), "processorActor")
val view = system.actorOf(Props(), "processorActor")

override protected implicit val executor: ExecutionContext = system.dispatcher
override protected val log: LoggingAdapter = Logging(system, getClass)
override protected implicit val materializer: ActorMaterializer = ActorMaterializer()

Http().bindAndHandle(routes(processor, view), httpInterface, httpPort)
}

trait BaseServiceRoute {
protected implicit def executor: ExecutionContext
protected implicit def materializer: ActorMaterializer
protected def log: LoggingAdapter
}

trait MainActorSystem {
implicit val system = ActorSystem("booking")
}

final case class CalculatePriceForRangeDto(unitId: Int, from: Long, to: Long)

trait PriceServiceRoute extends BaseServiceRoute {

implicit val timeout = Timeout(30 seconds)

import com.example.crudapi.utils.MarshallingSupport._

def customersRoute(command: ActorRef, query: ActorRef) = pathPrefix("price") {
post {
path("calculate") {
decodeRequest {
entity(as[CalculatePriceForRangeDto]) {
priceForRange => onComplete((query ? CalculatePriceForRange(

但我得到
Error:(32, 20) could not find implicit value for parameter um: akka.http.scaladsl.unmarshalling.FromRequestUnmarshaller[com.example.crudapi.http.routes.CalculatePriceForRangeDto]
entity(as[CalculatePriceForRangeDto]) {
^

看过所有相关的 SO 问题,但没有解决我的问题。奇怪的是,我尝试了 Typesafe 模板 akka-dddd-cqrs 并且它可以工作,代码相同。

我是否缺少带有隐式上下文的东西?
任何想法可能是什么?

最佳答案

SprayJsonSupport适用于 Spray-json(不适用于 json4s)。因此,您需要按如下方式定义编码器

trait JsonMarshallers extends DefaultJsonProtocol {
implicit val DigestItemWireFormat = jsonFormat6(DigestItemWire.apply)

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 %s on parsing of LocalDateTime type".format(x.getClass.getName))
}
}
}

然后导入 JsonMarshallers._在您使用它们的范围内或将其与 PriceServiceRoute 混合使用并导入 akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport._在文件的开头。

关于scala - Akka -http : could not find implicit value for parameter unmarshalling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33574176/

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