case class 个对象的编组列表
Marshalling list of case class objects
我想根据我的情况 return 个 json 个对象列表 class 个对象。
以下是我的喷雾路由器,其中 return 的 'Appointment' 个对象列表。
trait GatewayService extends HttpService with SLF4JLogging {
import com.sml.apigw.protocols.AppointmentProtocol._
import spray.httpx.SprayJsonSupport._
implicit def executionContext = actorRefFactory.dispatcher
val router =
pathPrefix("api" / "v1") {
path("appointments") {
get {
complete {
val a = new Appointment("1", "2")
val l = List(a, a, a, a)
l
}
}
}
}
}
}
以下是'AppointmentProtocol'
import spray.json.DefaultJsonProtocol
case class Appointment(id: String, patient: String)
object AppointmentProtocol extends DefaultJsonProtocol {
implicit val appointmentFormat = jsonFormat2(Appointment.apply)
}
它给出编译错误“List[Appointment] 的表达式类型未确认预期类型 toResponseMarshallable”
我认为你应该使用这个库 spray-json 根据你的 spay 版本这将是你的导入并且不要忘记将导入添加到你的代码中:
import MyJsonProtocol._
import spray.json._
为此请确保您已导入:
libraryDependencies += "io.spray" %% "spray-json" % "1.3.2"
然后你可以用这个转换一个对象,我也有问题 class 在同一个文件中,但这是使用测试:
case class Color(name: String, red: Int, green: Int, blue: Int)
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val colorFormat = jsonFormat4(Color)
}
import MyJsonProtocol._
import spray.json._
val json = Color("CadetBlue", 95, 158, 160).toJson
val color = json.convertTo[Color]
和列表:
val jsonAst = List(1, 2, 3).toJson
这是从spray-jsongithub project
中提取的例子
也许您在示例中遗漏了某些内容,但我的基于大脑的编译器告诉我您的代码应该抛出编译错误,因为 Spray 中的任何指令都需要 Route
类型的结果,正如我所见你有一个List[Appointment]
。请阅读关于路线的 this 文章。你的路线结构应该用 complete
完成,所以我认为这种方式可以解决你的问题:
get {
val a = new Appointment("1", "2")
val l = List(a, a, a, a)
complete(l)
}
请注意包装列表的 complete
指令。这应该会有所帮助,否则请通过使用标志 -Xprint:typer
编译代码来为树提供已解决的隐式问题,这应该会显示隐式问题出在哪里。
我想根据我的情况 return 个 json 个对象列表 class 个对象。
以下是我的喷雾路由器,其中 return 的 'Appointment' 个对象列表。
trait GatewayService extends HttpService with SLF4JLogging {
import com.sml.apigw.protocols.AppointmentProtocol._
import spray.httpx.SprayJsonSupport._
implicit def executionContext = actorRefFactory.dispatcher
val router =
pathPrefix("api" / "v1") {
path("appointments") {
get {
complete {
val a = new Appointment("1", "2")
val l = List(a, a, a, a)
l
}
}
}
}
}
}
以下是'AppointmentProtocol'
import spray.json.DefaultJsonProtocol
case class Appointment(id: String, patient: String)
object AppointmentProtocol extends DefaultJsonProtocol {
implicit val appointmentFormat = jsonFormat2(Appointment.apply)
}
它给出编译错误“List[Appointment] 的表达式类型未确认预期类型 toResponseMarshallable”
我认为你应该使用这个库 spray-json 根据你的 spay 版本这将是你的导入并且不要忘记将导入添加到你的代码中:
import MyJsonProtocol._
import spray.json._
为此请确保您已导入:
libraryDependencies += "io.spray" %% "spray-json" % "1.3.2"
然后你可以用这个转换一个对象,我也有问题 class 在同一个文件中,但这是使用测试:
case class Color(name: String, red: Int, green: Int, blue: Int)
object MyJsonProtocol extends DefaultJsonProtocol {
implicit val colorFormat = jsonFormat4(Color)
}
import MyJsonProtocol._
import spray.json._
val json = Color("CadetBlue", 95, 158, 160).toJson
val color = json.convertTo[Color]
和列表:
val jsonAst = List(1, 2, 3).toJson
这是从spray-jsongithub project
中提取的例子也许您在示例中遗漏了某些内容,但我的基于大脑的编译器告诉我您的代码应该抛出编译错误,因为 Spray 中的任何指令都需要 Route
类型的结果,正如我所见你有一个List[Appointment]
。请阅读关于路线的 this 文章。你的路线结构应该用 complete
完成,所以我认为这种方式可以解决你的问题:
get {
val a = new Appointment("1", "2")
val l = List(a, a, a, a)
complete(l)
}
请注意包装列表的 complete
指令。这应该会有所帮助,否则请通过使用标志 -Xprint:typer
编译代码来为树提供已解决的隐式问题,这应该会显示隐式问题出在哪里。