由于某些原因,我的代码不能解析这个符号"respondWithMediaType“,尽管有所有必要的导入。我对spray和scala都是新手--所以可能遗漏了一些明显的东西?
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)
}
}
}
}}
发布于 2015-09-10 01:14:11
据我所知,你想发回json,然后你不需要设置内容类型,set json会为你做这件事,用complete方法替换respondWithMediaType如下所示:
post {
entity(as[Users]) { users: Users =>
complete {
service.saveOrUpdate(users)
}
}
}并确保你得到了所有的喷雾-json导入,更多信息here
https://stackoverflow.com/questions/32485144
复制相似问题