首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >akka-http :未能找到参数解编组的隐式值

akka-http :未能找到参数解编组的隐式值
EN

Stack Overflow用户
提问于 2015-11-06 19:23:55
回答 1查看 8.4K关注 0票数 5

我的喷雾器支架看起来像这样

代码语言:javascript
复制
object MarshallingSupport extends SprayJsonSupport {
  implicit def json4sFormats: Formats = DefaultFormats
}

在我的路径中,我想将请求映射到dto。

代码语言:javascript
复制
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(

但我得到了

代码语言:javascript
复制
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]) {
               ^

看到了所有相关的问题,但没有解决我的问题。奇怪的是,我尝试了Typesafe模板,它工作,相同的代码。

我是不是漏掉了一些隐含的内容?有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-06 19:35:06

SprayJsonSupport适用于喷雾-json(而不是json4s)。因此,您需要定义编组器,如下所示

代码语言:javascript
复制
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._

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33574176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档