| | |
|
|
|
A ZRM Mini-tutorial
This tutorial provides a brief introduction to the Project Zero resource manager, ZRM, and illustrates how quickly you can get up and running RESTful data. In this mini-tutorial you will see that your:
- Database is automatically created from resource model definitions
- Data fixtures are loaded into the database, by default
- Application's default RESTful APIs can list, filter, retrieve, create, update, and delete with ZRM
Use the following steps:
- Create a new Project Zero application.
- Add a dependency on the
zero.resource in the ivy.xml file of the application as shown in the following example:
<dependency org="zero" name="zero.resource" rev="1.0+"/>
- Create the following new directories that are not created in the application template:
-
models - Create this directory under the
app directory: ${app_root}/app/models
-
fixtures - Create this directory under the newly created
models directory: ${app_root}/app/models/fixtures
- Create a resource model definition and data fixtures. Add a
person.groovy file to ${app_root}/app/models with the following contents:
fields = [
first_name:[type:'CharField'],
birth_date:[type:'DateField'],
is_child:[type: 'BooleanField'],
]
For more information, see the Resource Model declaration article.
- Add an
initial_data.json file to ${app_root}/app/models/fixtures with the following contents:
[
{
"type": "person",
"fields": {
"id" : "1",
"first_name" : "Tommy",
"birth_date" : "2005-12-12",
"is_child" : "0",
"updated" : "1962-09-24 03:23:34.234"
}
},
{
"type": "person",
"fields": {
"id" : "2",
"first_name" : "Billy",
"birth_date" : "1983-04-10",
"is_child" : "1",
"updated" : "1983-09-23 03:23:34.234"
}
}
]
For more information about how ZRM automatically generates the database, see the Database Generation information. Also see the Loading initial data fixtures information for more information about how ZRM loads the initial data.
- Create the
person.groovy resource handler file in ${app_root}/app/resources with the following code:
For more information about enabling HTTP REST access, see the Enabling HTTP access information.
- Start the Project Zero application.
Testing your application data
- Point your browser to http://localhost:8080/resources/person and confirm that an array JSON representation with the data in the
initial_data.json file is returned.
Note: the JSON representation in this test and subsequent tests should not be in identical format.
- Point your browser to http://localhost:8080/resources/person/1 and confirm that an object JSON representation is returned with
id equals 1.
- Point your browser to http://localhost:8080/resources/person?first_name__startswith=B and confirm that an object JSON representation is returned with
id equals 2.
For more information, see the Reading and writing collection members and filtering resource collections sectons.
|
|
r2 - 09 Feb 2008 - 17:19:25 - paynel
|
|
|
| | |