无法解析符号respondWithMediaType - Scala / Spray / Json

时间:2015-09-09 16:44:33

标签: scala spray spray-json

出于某种原因,我的代码不会解决这个符号“respondWithMediaType”,尽管有所有必要的导入。我对喷雾和斯卡拉都很陌生 - 所以也许缺少明显的东西?

 import spray.http.MediaTypes._
 import spray.json._
 import DefaultJsonProtocol._
 .....
 trait Service extends CassandraSpec with UsersService {
   implicit val system: ActorSystem

   implicit def executor: ExecutionContextExecutor

   implicit val materializer: Materializer
   implicit val timeout: Timeout
   implicit val jsonFormatUsers = jsonFormat5(Users)
   implicit val jsonFormatAllUsers = List(jsonFormat5(Users))
   .....

 pathPrefix("users") {
    (get & path(Segment)) { email =>
        respondWithMediaType(MediaTypes.`application/json`) {
        service.getByUsersEmail(email)
      }
    }
    get {
      // GET /users
      path(Rest) {
        respondWithMediaType(`application/json`) {
          service.getAllUsers()
        }
      }
    } ~
    post {
        entity(as[Users]) { users: Users =>
          respondWithMediaType(`application/json`) {
            service.saveOrUpdate(users)
          }
        }
      }
  }

}

1 个答案:

答案 0 :(得分:0)

据我了解你要发回json然后你不需要设置内容类型,spray-json会为你做, 使用respondWithMediaType方法替换complete,如下所示:

post {
        entity(as[Users]) { users: Users =>
          complete {
            service.saveOrUpdate(users)
          }
        }
      }

并确保您获得所有spray-json导入,更多信息here