PUT requests
PUT is for updating objects. It's not really different from POST from developer's point of view. It's just that you get an identifier of the object from request.
(I already did that for Spring 3, you may want to take a look: REST with Spring 3: PUT.)
Retrieve the request
package bootstrap.liftweb
import me.m1key.rest.DogsRestService
import net.liftweb.http._
class Boot {
def boot {
// where to search snippet
LiftRules.addToPackages("me.m1key")
LiftRules.statelessDispatchTable.append {
// ...
case request @ Req(List("rest", "dogs"), _, PutRequest) =>
() => DogsRestService.updateDog(request)
}
}
}
It's very similar to POST, so you can check out the POST post (really cunning game play here, folks!) for more details.
Processing the request
Processing the request is very similar to the PUT case.
def updateDog(request: Req): Box[LiftResponse] = {
var dogId = ""
var dogName = ""
request.xml match {
case Full(<dog>{parameters @ _*}</dog>) => {
for(parameter <- parameters){
parameter match {
case <name>{name}</name> => dogName = name.text
case <id>{id}</id> => dogId = id.text
case _ =>
}
}
val dog = new Dog(dogId, dogName)
Log.info("Updating a dog with id: " + dog.id)
return Full(InMemoryResponse(dog.toXml.toString.getBytes("UTF-8"), List("Content-Type" -> "text/xml"), Nil, 200))
}
case _ => Log.error("Invalid request");
Log.error("Request: " + request);
Log.error("Request.xml: " + request.xml);Full(BadResponse())
}
}
Summary
Similarly to the POST case, handling RESTful PUT requests in Lift 1.0 is not as comfortable as it is with Spring 3. I'm looking forward to seeing Lift 2.0 improvements in that matter.
Download source code for this article

0 comments:
Post a Comment