3

我正在使用喷雾,我需要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
          }
        }
      }
    }

它给出了编译错误could not find implicit value for parameter marshaller: spray.httpx.marshalling.ToResponseMarshaller

4

1 回答 1

0

您需要告诉 Spray 它应该如何序列化您的案例类。

只需配置类似

object JsonSupport {
    implicit val formatConfigResults = jsonFormat3(ConfigResults)
}

jsonFormat'number' 中的数字代表您的案例类中的成员数。

然后你只需要导入你的路由,你定义这个隐式的类。

import JsonSupport._
于 2014-02-28T09:20:26.163 回答