@MiloXia
2015-08-07T15:36:10.000000Z
字数 2685
阅读 1747
开发
$ ssh qiaobutang@10.1.1.223
$ cd apptest
$ jps -lvm | grep 8888
$ kill <pid>
$ ./start_resume.sh
Notice:
case req @ Req("group" :: ObjectIdExtractor(groupId) :: "post" :: Nil, "json", PostRequest)
subject <- req.param("subject").failMsg("请输入主题")
如下方式会存在问题
case req @ Req("jobs" :: suffix :: Nil, "json", GetRequest) if suffix == "recommend"
//warning 如果不加守卫,可能会把别的API(如: jobs/apply/.. )给匹配到
val Suffix = "recommend"
case req @ Req("jobs" :: Suffix :: Nil, "json", GetRequest)
一个api 返回的基本json结构
{
"resultCode": status code
"resultBody": {
...
}
....
}
每个API的返回值类型为Box[JObject]
type DispatchPF = PartialFunction[Req, Box[JObject]]
返回方式
//1. Full
Full(error ~ ("failureCauses" -> fieldError.map(_.msg.toString())))
//2. Box.legacyNullTest
Box.legacyNullTest {
"categories" -> BBSGroup.Categories.values.toList.sortBy(_.id).map(categoryInfo)
}
//3. Box.map
getAccountId_!(req).map { accountId =>
("json" -> "ok")
}
Notice: 目前还未有需要主动返回403的情况
触发原因:
基本结构:
{
"resultCode": 400
"failureCause" : "...."
}
Notice: 建议调用errorWith(msg)方法取代直接返回 error ~ ("failureCause" -> msg)
对于一些resource/{id}的请求,在查找不到(包含逻辑删除)该id的对象时返回notFound
case req @ Req("group" :: "post" :: ObjectIdExtractor(postId) :: "like" :: Nil, "json", PostRequest) =>
getAccountId_!(req).map { accountId =>
BBSPost.getLiveById(postId).map[JObject] { post =>
...
}.getOrElse(notFound)
}
自定义: 和client端商量后选取一个code, 作为某个api的返回值
作用域: 仅限于单个api中
complete(406) //告诉client端,重复操作了
Notice: 加注释
对于Post等不需要返回数据的请求, 返回NoMessage
case req @ Req("group" :: ObjectIdExtractor(groupId) :: "join" :: Nil, "json", PostRequest) =>
getAccountId_!(req).map { accountId =>
...
NoMessage
}
errorWith(msg)
error ~ ("failureCause" -> msg)
error ~ ("failureCauses" -> fieldError.map(_.msg.toString()))
com.qiaobutang.resume.app
|_App.scala (主入口)
|_api (存放resource)
|_Group.scala
|_Job.scala
|_Career.scala
|_....scala
object Resource extends AppResource {
...
}
val serve: DispatchPF = {
...
}
val serve: DispatchPF = {
case Req("path" :: Nil, "json", GetRequest) => {
...
}
...
override val services: LiftRules.DispatchPF =
List(..., Resource).map { r => ...
获取默认分辨率
defaultResolution(req: Req)
获取用户头像,性别,昵称等社交信息
Scene.getSocialProfile(resolution: String)(accountId: ObjectId)
Group.uploadImageAndGetIds(req: Req, imageModel: MongoModelMeta[T], accountId: ObjectId)