我正在使用 spray,我需要通过一个方法返回一个 json
对象。
val route =
path("all-modules") {
get {
respondWithMediaType(`text/html`) {
complete( configViewer.findAllModules.toString)
}
}
}
这打印 ConfigResults(S1000,Success,List(testDataTypes, mandate, sdp))
但我需要将其作为 json
对象获取。我该怎么做?
我试过这种方式
val route =
path("all-modules") {
get {
respondWithMediaType(`application/json`) {
complete{
configViewer.findAllModules
}
}
}
}
它给出编译错误无法找到参数编码器的隐式值:spray.httpx.marshalling.ToResponseMarshaller
最佳答案
您需要告诉 Spray 它应该如何序列化您的案例类。
只需配置类似
object JsonSupport {
implicit val formatConfigResults = jsonFormat3(ConfigResults)
}
jsonFormat'number'中的数字代表你的案例类中的成员数。
然后你只需要导入你的路由,你定义这个隐式的类。
import JsonSupport._
关于json - 使用 spray.json 获取 Json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22090671/