在 akka-http Scala 中注册泛型到 spray-json-support 的正确方法是什么?
What is the correct way to register generic type to spray-json-support in akka-http Scala?
所以,我有这个 class:
case class Something[T](data: Option[T] = None)
我按照https://github.com/spray/spray-json and in https://doc.akka.io/docs/akka-http/current/common/json-support.html中的说明注册了它。像这样:
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json.DefaultJsonProtocol
trait InternalJsonFormat extends SprayJsonSupport with DefaultJsonProtocol {
import spray.json._
implicit def somethingFormat[A :JsonFormat] = jsonFormat1(Something.apply[A])
}
最后我使用了来自 akka http 指令的 complete
。像这样:
import akka.http.scaladsl.server.Directives._
object ResponseIt extends InternalJsonFormat {
def apply[T](rawData: T) = {
val theResponse = Something(data = Some(rawData))
complete(theResponse)
}
}
然后我在 complete(theResponse)
中收到错误消息。它说
Type mismatch, expected: ToResponseMarshallable, actual: Something[T]
============================================= ==============
我已尝试编辑最后的代码以进行调试,如下所示:
object ResponseIt extends InternalJsonFormat {
import spray.json._
def apply[T](rawData: T) = {
val theResponse = Something(data = Some(rawData))
val trying = theResponse.toJson
complete(theResponse)
}
}
并在 val trying = theResponse.toJson
中得到新的错误。像这样:
No implicits found for parameter writer: JsonWriter[Something[T]]
所以,我真的很困惑我的代码有什么问题?。有没有正确的方法来使用 akka http 中的 spray json 支持?
提前致谢
你看,这里没有证据表明你的 T
存在 JsonFormat
:
def apply[T](rawData: T) = {
// ^--- here
val theResponse = Something(data = Some(rawData))
val trying = theResponse.toJson
complete(theResponse)
}
可以重写此方法以提供 JsonFormat
泛型 T
:
def apply[T](rawData: T)(implicit formatter: JsonFormat[T])
所以,我有这个 class:
case class Something[T](data: Option[T] = None)
我按照https://github.com/spray/spray-json and in https://doc.akka.io/docs/akka-http/current/common/json-support.html中的说明注册了它。像这样:
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import spray.json.DefaultJsonProtocol
trait InternalJsonFormat extends SprayJsonSupport with DefaultJsonProtocol {
import spray.json._
implicit def somethingFormat[A :JsonFormat] = jsonFormat1(Something.apply[A])
}
最后我使用了来自 akka http 指令的 complete
。像这样:
import akka.http.scaladsl.server.Directives._
object ResponseIt extends InternalJsonFormat {
def apply[T](rawData: T) = {
val theResponse = Something(data = Some(rawData))
complete(theResponse)
}
}
然后我在 complete(theResponse)
中收到错误消息。它说
Type mismatch, expected: ToResponseMarshallable, actual: Something[T]
============================================= ==============
我已尝试编辑最后的代码以进行调试,如下所示:
object ResponseIt extends InternalJsonFormat {
import spray.json._
def apply[T](rawData: T) = {
val theResponse = Something(data = Some(rawData))
val trying = theResponse.toJson
complete(theResponse)
}
}
并在 val trying = theResponse.toJson
中得到新的错误。像这样:
No implicits found for parameter writer: JsonWriter[Something[T]]
所以,我真的很困惑我的代码有什么问题?。有没有正确的方法来使用 akka http 中的 spray json 支持?
提前致谢
你看,这里没有证据表明你的 T
存在 JsonFormat
:
def apply[T](rawData: T) = {
// ^--- here
val theResponse = Something(data = Some(rawData))
val trying = theResponse.toJson
complete(theResponse)
}
可以重写此方法以提供 JsonFormat
泛型 T
:
def apply[T](rawData: T)(implicit formatter: JsonFormat[T])