spray-json无法编组Map [String,String]

时间:2013-09-11 20:40:45

标签: scala spray-json

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

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")
            }
          }
        }
    }
}

根据this test我应该能够在导入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]

不确定是什么问题:(

1 个答案:

答案 0 :(得分:20)

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

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

现在一切都很好。