繁体   English   中英

spray-json错误:无法找到参数um的隐含值

[英]spray-json error: could not find implicit value for parameter um

我有这个案例课

case class Person(val name: String)

object JsonImplicits extends DefaultJsonProtocol {
  implicit val impPerson = jsonFormat1(Person)
}

我正在尝试使用spray-json来解析post请求:

  post {
    entity(as[Person]) { person =>
      complete(person)
    }
  }

但是当我尝试编译时,我得到了:

src / main / scala / com / example / ServiceActor.scala:61:错误:找不到参数um的隐式值:spray.httpx.unmarshalling.FromRequestUnmarshaller [com.example.Person]

我不明白发生了什么,我怎么能解决这个问题呢?

谢谢

Spray的'entity [E]'指令在其类型E的范围内需要隐式编组器JsonImplicits对象为类型E创建json marshaller和unmarshaller。

您需要确保implicit val impPerson在范围内,换句话说,将import JsonImplicits._放在路由定义之上。

package abc.json

import spray.json.DefaultJsonProtocol


object OrderJsonProtocol extends DefaultJsonProtocol {

  implicit val orderFormat = jsonFormat1(Order)
}


case class Order(orderNumber: String)

import akka.actor.Actor
import abc.json._
import spray.routing.HttpService

class OrderRestServiceActor extends Actor with HttpService {

  def actorRefFactory = context

  def receive = runRoute(route)



  val route = {
    import OrderJsonProtocol._
    import spray.httpx.SprayJsonSupport.sprayJsonUnmarshaller


    path("order") {
      post {
        println("inside the path")
        entity(as[Order]) { order =>
         complete(s"OrderNumber: ${order.orderNumber}")
        }

      }
    }

  }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM