如何在RestHelper中的lift框架中简单地访问get和post属性?文档中没有任何明确的例子:(
package my.domain
import net.liftweb.http._
import net.liftweb.http.rest._
import net.liftweb.json.JsonAST._
import net.liftweb.json._
import net.liftweb.common.{Box,Full,Empty,Failure,ParamFailure}
import net.liftweb.mapper._
import ru.dmteam.model.{RssItem}
object ContentRest extends RestHelper {
def getq: String = {
val q = S.param("q")
q.toString
}
serve {
case "api" :: "static" :: _ XmlGet _=> <b>{getq}</b>
}
}
Run Code Online (Sandbox Code Playgroud)
我想了解如何q在我请求时提升电梯的价值http://localhost:8080/api/static.xml?q=test
Dav*_*lak 23
Lift使用Box而不是null来指示是否传递了参数.这样可以很好地利用Scala进行理解,将一个好的请求处理程序链接在一起.我会让代码说明一切:
object MyRest extends RestHelper {
// serve the q parameter if it exists, otherwise
// a 404
serve {
case "api" :: "x1" :: _ Get _ =>
for {
q <- S.param("q")
} yield <x>{q}</x>
}
// serve the q parameter if it exists, otherwise
// a 404 with an error string
serve {
case "api" :: "x2" :: _ Get _ =>
for {
q <- S.param("q") ?~ "Param 'q' missing"
} yield <x>{q}</x>
}
// serve the q parameter if it exists, otherwise
// a 401 with an error string
serve {
case "api" :: "x2" :: _ Get _ =>
for {
q <- S.param("q") ?~ "Param 'q' missing" ~> 401
} yield <x>{q}</x>
}
// serve the q, r, s parameters if this exists, otherwise
// different errors
serve {
case "api" :: "x3" :: _ Get _ =>
for {
q <- S.param("q") ?~ "Param 'q' missing" ~> 401
r <- S.param("r") ?~ "No 'r'" ~> 502
s <- S.param("s") ?~ "You're s-less" ~> 404
} yield <x><q>{q}</q><r>{r}</r><s>{s}</s></x>
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定,但你能试试吗?
S.param("param_name")
Run Code Online (Sandbox Code Playgroud)
http://scala-tools.org/mvnsites-snapshots/liftweb/scaladocs/index.html
或者使用req类
case r @ JsonPost("some" :: "path" :: _, json) => _ => {
r.param("name")
}
Run Code Online (Sandbox Code Playgroud)
http://scala-tools.org/mvnsites-snapshots/liftweb/scaladocs/index.html
编辑:我有这个示例运行:
package code.rest
import net.liftweb.http.rest._
object SampleRest extends RestHelper {
serve {
case Get("sample" :: _, req) =>
<hello>{req.param("name") getOrElse ("??") }</hello>
}
}
Run Code Online (Sandbox Code Playgroud)