June 2013
Intermediate to advanced
253 pages
5h 5m
English
This chapter looks at recipes around REST web services, via Lift’s RestHelper trait. For an introduction, take a look at the Lift wiki page and Chapter 5 of Simply Lift.
The sample code from this chapter is at https://github.com/LiftCookbook/cookbook_rest.
You find yourself repeating parts of URL paths in your RestHelper and
you Don’t want to Repeat Yourself (DRY).
Use prefix in your RestHelper:
packagecode.restimportnet.liftweb.http.rest.RestHelperimportnet.liftweb.http.LiftRulesobjectIssuesServiceextendsRestHelper{definit():Unit={LiftRules.statelessDispatch.append(IssuesService)}serve("issues"/"by-state"prefix{case"open"::NilXmlGet_=><p>Noneopen</p>case"closed"::NilXmlGet_=><p>Noneclosed</p>case"closed"::NilXmlDelete_=><p>Alldeleted</p>})}
This service responds to URLs of /issues/by-state/open and /issues/by-state/closed and we have
factored out the common part as a prefix.
Wire this into Boot.scala with:
importcode.rest.IssuesServiceIssuesService.init()
We can test the service with cURL:
$ curl -H 'Content-Type: application/xml'
http://localhost:8080/issues/by-state/open
<?xml version="1.0" encoding="UTF-8"?>
<p>None open</p>
$ curl -X DELETE -H 'Content-Type: application/xml'
http://localhost:8080/issues/by-state/closed
<?xml version="1.0" encoding="UTF-8"?>
<p>All deleted</p>You can have many serve blocks in your RestHelper, which helps give
your REST service structure.
In this example, ...
Read now
Unlock full access