Changes for page Your First Rest Project
Last modified by Steve Peery on 2013/09/06 11:02
From version 10.1
edited by Pascal Robert
on 2011/12/27 14:28
on 2011/12/27 14:28
Change comment:
There is no comment for this version
To version 11.1
edited by Pascal Robert
on 2011/12/27 22:21
on 2011/12/27 22:21
Change comment:
There is no comment for this version
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -24,7 +24,6 @@ 24 24 | id | integer | primary key 25 25 | title | string(255) | 26 26 | content | string(4000) | 27 -| lastModified | timestamp | 28 28 | creationDate | timestamp | 29 29 | author | integer | relation with Author 30 30 ... ... @@ -35,7 +35,6 @@ 35 35 | firstName | string(50) | 36 36 | lastName | string(50) | 37 37 | email | string(100) | unique 38 -| passwd | string(16) | 39 39 40 40 == Creating the EOModel == 41 41 ... ... @@ -75,7 +75,6 @@ 75 75 76 76 |= Attribute name |= Column |= Prototype 77 77 | content | content | longtext 78 -| lastModified | lastModified | dateTime 79 79 | creationDate | creationDate | dateTime 80 80 81 81 If you did everything well, the list of attributes should look like this: ... ... @@ -88,7 +88,6 @@ 88 88 | firstName | firstName | varchar50 89 89 | lastName | lastName | varchar50 90 90 | email | email | varchar100 91 -| passwd | passwd | varchar16 92 92 93 93 Final list of attributes should look like this: 94 94 ... ... @@ -110,8 +110,22 @@ 110 110 111 111 {{/code}} 112 112 113 -Nothing fancy here. Now ,let'suse migrationstoactuallycreate thedatabase.109 +Nothing fancy here. Now open **BlogEntry.java** and add the following method: 114 114 111 +{{code}} 112 + 113 + @Override 114 + public void awakeFromInsertion(EOEditingContext editingContext) { 115 + super.awakeFromInsertion(editingContext); 116 + this.setCreationDate(new NSTimestamp()); 117 + } 118 + 119 +{{/code}} 120 + 121 +Why are we adding this? **awakeFromInsertion** is a very good way of setting default values when creating a new instance of a Enterprise Object (EO). In this case, we want to set automatically the creation date without having the user to add that value. 122 + 123 +Now, let's use migrations to actually create the database. 124 + 115 115 == Using migrations == 116 116 117 117 Migrations allow you to create the tables and columns (and some types of constraint). **Entity Modeler** have support to generate the code for the first migration, which is called "migration 0". To do that, open the EOModel (**BlogModel EOModel** in the **Resources** folder), right-click on the model name and select **Generate Migration**. ... ... @@ -147,9 +147,9 @@ 147 147 {{code}} 148 148 149 149 BlogRest[62990] INFO er.extensions.migration.ERXMigrator - Upgrading BlogModel to version 0 with migration 'your.app.model.migrations.BlogModel0@4743bf3d' 150 -BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing CREATE TABLE Author(email VARCHAR(100) NOT NULL, firstName VARCHAR(50) NOT NULL, id INTEGER NOT NULL, lastName VARCHAR(50) NOT NULL , passwd VARCHAR(16)NOT NULL)160 +BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing CREATE TABLE Author(email VARCHAR(100) NOT NULL, firstName VARCHAR(50) NOT NULL, id INTEGER NOT NULL, lastName VARCHAR(50) NOT NULL) 151 151 BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing ALTER TABLE Author ADD PRIMARY KEY (id) 152 -BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing CREATE TABLE BlogEntry(authorID INTEGER NOT NULL, content TIMESTAMP NOT NULL, creationDate TIMESTAMP NOT NULL, id INTEGER NOT NULL, lastModified TIMESTAMP NOT NULL,title VARCHAR(255) NOT NULL)162 +BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing CREATE TABLE BlogEntry(authorID INTEGER NOT NULL, content TIMESTAMP NOT NULL, creationDate TIMESTAMP NOT NULL, id INTEGER NOT NULL, title VARCHAR(255) NOT NULL) 153 153 BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing ALTER TABLE BlogEntry ADD PRIMARY KEY (id) 154 154 BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing ALTER TABLE BlogEntry ADD CONSTRAINT "FOREIGN_KEY_BLOGENTRY_AUTHORID_AUTHOR_ID" FOREIGN KEY (authorID) REFERENCES Author (id) 155 155 BlogRest[62990] DEBUG NSLog - evaluateExpression: <er.h2.jdbcadaptor.ERH2PlugIn$H2Expression: "UPDATE _dbupdater SET version = ? WHERE modelname = ?" withBindings: 1:0(version), 2:"BlogModel"(modelName)> ... ... @@ -184,8 +184,114 @@ 184 184 185 185 ERRest needs controllers to act as a broker between working with the objects and the routes. So let's create a controller for BlogEntry. 186 186 197 +Create a Java class named **BlogEntryController**, in the **your.app.rest.controllers** package, that will extend from **er.rest.routes.ERXDefaultRouteController**. Click **Finish**. 198 + 199 +When you extend from **ERXDefaultRouteController**, a bunch of methods are added to the subclass. Let's see what they are for. 200 + 201 +* **updateAction**: to update a specific instance of BlogEntry 202 +* **destroyAction**: to delete a specific instance of BlogEntry 203 +* **showAction**: to get one specific instance of BlogEntry 204 +* **createAction**: to create a new object (a new instance of BlogEntry) 205 +* **indexAction**: to list all (or a sublist) of the objects. 206 + 207 +{{info}} 208 +In Project Wonder, *Action* at the end of a method is a convention for REST and Direct Actions, when you call those methods from certain components, you don't need to add the *Action* part. 209 +{{/info}} 210 + 211 +For this tutorial, we will implement the **createAction** and **showAction** methods. But first, we need to create a key filter. A key filter will... filter the input and the output of REST request so that you don't have to send all attributes for a blog entry. For example, we want to show the details for an author, but we don't want to show the password for the author (in real-life, the password would be encrypted)! 212 + 213 +Add this method in **BlogEntryController**: 214 + 215 +{{code}} 216 + 217 + protected ERXKeyFilter filter() { 218 + ERXKeyFilter personFilter = ERXKeyFilter.filterWithAttributes(); 219 + personFilter.setAnonymousUpdateEnabled(true); 220 + 221 + ERXKeyFilter filter = ERXKeyFilter.filterWithAttributes(); 222 + filter.include(BlogEntry.AUTHOR, personFilter); 223 + filter.setUnknownKeyIgnored(true); 224 + 225 + return filter; 226 + } 227 + 228 +{{/code}} 229 + 230 +Now, let's implement the **creationAction** method: 231 + 232 +{{code}} 233 + 234 + public WOActionResults createAction() throws Throwable { 235 + BlogEntry entry = create(filter()); 236 + editingContext().saveChanges(); 237 + return response(entry, filter()); 238 + } 239 + 240 +{{/code}} 241 + 242 +In 3 lines of code, you can create an object based on the request, save the new object to the database and return the new object in the response. Not bad, eh? 243 + 244 +Last step in the controller: implementing the **showAction** method. Again, the code is simple: 245 + 246 +{{code}} 247 + 248 + public WOActionResults indexAction() throws Throwable { 249 + NSArray<BlogEntry> entries = BlogEntry.fetchAllBlogEntries(editingContext()); 250 + return response(entries, filter()); 251 + } 252 + 253 +{{/code}} 254 + 255 +That code simply fetch all blog entries and return them in the response. 256 + 257 +We can now go to the next step: adding the routes. 258 + 187 187 == Adding the routes == 188 188 261 +A route in ERRest is simply a way to define the URL for the entities and to specify which controller the route should use. When your controller extends from **ERXDefaultRouteController**, it's easy to register a controller and a route. In **Application.java**, in the **Application** constructor, add the following code: 262 + 263 +{{code}} 264 + 265 + ERXRouteRequestHandler restRequestHandler = new ERXRouteRequestHandler(); 266 + restRequestHandler.addDefaultRoutes(BlogEntry.ENTITY_NAME); 267 + ERXRouteRequestHandler.register(restRequestHandler); 268 + setDefaultRequestHandler(restRequestHandler); 269 + 270 +{{/code}} 271 + 272 +The **addDefaultRoutes** method do all of the required magic, and use convention. That's why we had to name the controller **BlogEntryController**, because the convention is <EntityName>Controller. 273 + 274 +We are now reading to add and list blog postings! Start the application and take notice of the URL. It should be something like //http:~/~/yourip:someport/cgi-bin/WebObjects/BlogRest.woa// 275 + 189 189 == Adding posts and authors with curl == 190 190 278 +Since we didn't implement any HTML for our REST routes, we will create blog entries with //curl//, an open source HTTP client that is bundled with Mac OS X (you can use another client, like wget, if you like too). So let's create a blog entry. 279 + 280 +To create a blog entry, you need to use the POST HTTP method. We will use JSON as the format since it's a bit less chatty than XML. So if the URL to the application is //http:~/~/192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa_, the full //curl// command will be~:// 281 + 282 +{{code}} 283 +curl -X POST -v -d '{ "title": "First post", "content": "Some text", "author": { "firstName": "Pascal", "lastName": "Robert", "email": "probert@macti.ca" } }' http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.json 284 +{{/code}} 285 + 286 +The response should look this: 287 + 288 +{{code}} 289 + 290 +HTTP/1.0 201 Apple WebObjects 291 +Content-Length: 249 292 +x-webobjects-loadaverage: 0 293 +Content-Type: application/json 294 + 295 +{"id":1,"type":"BlogEntry","content":"Some text","creationDate":"2011-12-27T21:59:08Z","title":"First post","author":{"id":1,"type":"Author","email":"probert@macti.ca","firstName":"Pascal","lastName":"Robert"}} 296 + 297 +{{/code}} 298 + 299 +To get a list of blog entries: 300 + 301 +{{code}} 302 + 303 +curl -X GET http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.json 304 + 305 +{{/code}} 306 + 191 191 == Adding HTML views for blog posts ==