10

我有以下路线设置,但是当我的地图在第一个完整块中返回时,我收到一个错误:

could not find implicit value for evidence parameter of type spray.httpx.marshalling.Marshaller[scala.collection.immutable.Map[String,String]]

import spray.routing.HttpService
import akka.actor.Actor
import spray.http.HttpRequest
import spray.routing.RequestContext
import spray.json.DefaultJsonProtocol._


class UserServiceActor extends Actor with RestUserService {
  def actorRefFactory = context
  def receive = runRoute(linkRoute)

}

trait RestUserService extends HttpService {

  val userService = new LinkUserService

  def linkRoute = 
    pathPrefix("user" / Segment) {
      userId =>
        path("link") {
          parameters('service ! "YT") {
            complete {
              Map("status"-> "OK", "auth_url" -> "http://mydomain.com/auth")
            }
          }
        }
    }
}

根据这个测试,我应该能够在导入 DefaultJsonProtocol._ 时将 Map 转换为 json,但即使这样也失败了:

val map:Map[String, String] = Map("hi"->"bye")
map.toJson

Cannot find JsonWriter or JsonFormat type class for scala.collection.mutable.Map[String,String]

不知道出了什么问题:(

4

1 回答 1

22

喷雾邮件列表中的某个人指出,正在创建的 Map 是一个可变的,spray-json 不会编组它。我将其更改为scala.collection.immutable.Map并添加了以下导入:

import spray.httpx.SprayJsonSupport._
import spray.json.DefaultJsonProtocol._

现在一切都很好。

于 2013-09-13T18:13:35.483 回答