Changes for page Your First Rest Project
Last modified by Steve Peery on 2013/09/06 11:02
To version 21.1
edited by Filippo Laurìa
on 2013/07/22 12:25
on 2013/07/22 12:25
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. skcodes1 +XWiki.filippolauria - Content
-
... ... @@ -1,3 +1,5 @@ 1 + 2 + 1 1 {{toc/}} 2 2 3 3 = Introduction = ... ... @@ -120,6 +120,8 @@ 120 120 121 121 The model should show up in a window that looks like this: 122 122 125 +[[image:attach:EOModeler.png]] 126 + 123 123 If it didn't show up, the window might have opened behind the main Eclipse window. If that's the case, open the **Window** menu and select the windows that have //Entity Modeler// in its name. 124 124 125 125 In the Entity Modeler window, click on **Default**, and for the **URL** field, type ... ... @@ -227,7 +227,6 @@ 227 227 What we are going to do is to write a simple method that returns the full name of an author, e.g. a method that simply concatenate the first name, a space and the last name of the author. To do so, double-click on **Author.java** and add the following methods: 228 228 229 229 {{code}} 230 - 231 231 public String fullName() { 232 232 return this.firstName() + " " + this.lastName(); 233 233 } ... ... @@ -237,7 +237,6 @@ 237 237 Nothing fancy here. Now open **BlogEntry.java** and add the following method: 238 238 239 239 {{code}} 240 - 241 241 @Override 242 242 public void awakeFromInsertion(EOEditingContext editingContext) { 243 243 super.awakeFromInsertion(editingContext); ... ... @@ -267,7 +267,6 @@ 267 267 Remove the pound char in front of those two properties: 268 268 269 269 {{code}} 270 - 271 271 #er.migration.migrateAtStartup=true 272 272 #er.migration.createTablesIfNecessary=true 273 273 ... ... @@ -276,7 +276,6 @@ 276 276 After removing the pound char, the two properties should look like this: 277 277 278 278 {{code}} 279 - 280 280 er.migration.migrateAtStartup=true 281 281 er.migration.createTablesIfNecessary=true 282 282 ... ... @@ -285,7 +285,6 @@ 285 285 You are now ready to start the application so that it creates the database! To do so, right-click on **Application.java** (in the **your.app** folder) and select **Run As** -> **WOApplication**. In Eclipse's Console tab, you should see some output, including something similar to: 286 286 287 287 {{code}} 288 - 289 289 BlogRest[62990] INFO er.extensions.migration.ERXMigrator - Upgrading BlogModel to version 0 with migration 'your.app.model.migrations.BlogModel0@4743bf3d' 290 290 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) 291 291 BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing ALTER TABLE Author ADD PRIMARY KEY (id) ... ... @@ -341,7 +341,6 @@ 341 341 Add this method in **BlogEntryController**: 342 342 343 343 {{code}} 344 - 345 345 protected ERXKeyFilter filter() { 346 346 ERXKeyFilter personFilter = ERXKeyFilter.filterWithAttributes(); 347 347 personFilter.setAnonymousUpdateEnabled(true); ... ... @@ -358,7 +358,6 @@ 358 358 Now, let's implement the **createAction** method: 359 359 360 360 {{code}} 361 - 362 362 public WOActionResults createAction() throws Throwable { 363 363 BlogEntry entry = create(filter()); 364 364 editingContext().saveChanges(); ... ... @@ -372,7 +372,6 @@ 372 372 Last step in the controller: implementing the **indexAction** method. Again, the code is simple: 373 373 374 374 {{code}} 375 - 376 376 public WOActionResults indexAction() throws Throwable { 377 377 NSArray<BlogEntry> entries = BlogEntry.fetchAllBlogEntries(editingContext()); 378 378 return response(entries, filter()); ... ... @@ -389,7 +389,6 @@ 389 389 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: 390 390 391 391 {{code}} 392 - 393 393 ERXRouteRequestHandler restRequestHandler = new ERXRouteRequestHandler(); 394 394 restRequestHandler.addDefaultRoutes(BlogEntry.ENTITY_NAME); 395 395 ERXRouteRequestHandler.register(restRequestHandler); ... ... @@ -399,7 +399,7 @@ 399 399 400 400 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. 401 401 402 -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_>>url:http://yourip :someport/cgi-bin/WebObjects/BlogRest.woa_||shape="rect"]]397 +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_>>url:http://youripsomeport||shape="rect"]] 403 403 404 404 == Adding posts and authors with curl == 405 405 ... ... @@ -414,7 +414,6 @@ 414 414 The response should look this: 415 415 416 416 {{code}} 417 - 418 418 HTTP/1.0 201 Apple WebObjects 419 419 Content-Length: 249 420 420 x-webobjects-loadaverage: 0 ... ... @@ -427,7 +427,6 @@ 427 427 To get a list of blog entries: 428 428 429 429 {{code}} 430 - 431 431 curl -X GET http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.json 432 432 433 433 {{/code}} ... ... @@ -439,7 +439,6 @@ 439 439 Now, let's build a HTML view for blog posts (you don't want your readers to get your posts by JSON, right?). Again, we will use convention to make it work easily. Open up **BlogEntryController** and add the following method: 440 440 441 441 {{code}} 442 - 443 443 @Override 444 444 protected boolean isAutomaticHtmlRoutingEnabled() { 445 445 return true; ... ... @@ -452,7 +452,6 @@ 452 452 The next step to get it to work is to make **BlogEntryIndexPage** to implement the **er.rest.routes.IERXRouteComponent** interface. 453 453 454 454 {{code}} 455 - 456 456 import er.rest.routes.IERXRouteComponent; 457 457 458 458 public class BlogEntryIndexPage extends WOComponent implements IERXRouteComponent { ... ... @@ -462,7 +462,6 @@ 462 462 So now, the automatic HTML routing will send the request for **ra/blogEntries.html** to the **BlogEntryIndexPage** component. But we don't have any content in this component, so let's make a method to fetch all blog entries per creation date in descending order. So in **BlogEntryIndexPage.java**, add the following method: 463 463 464 464 {{code}} 465 - 466 466 public NSArray<BlogEntry> entries() { 467 467 EOEditingContext ec = ERXEC.newEditingContext(); 468 468 return BlogEntry.fetchAllBlogEntries(ec, BlogEntry.CREATION_DATE.descs()); ... ... @@ -473,7 +473,6 @@ 473 473 We need to use that method in a WORepetition, and for that loop, we need a BlogEntry variable to iterate in the list, so add the following code to **BlogEntryIndexPage.java**: 474 474 475 475 {{code}} 476 - 477 477 private BlogEntry entryItem; 478 478 479 479 public BlogEntry entryItem() { ... ... @@ -489,7 +489,6 @@ 489 489 The Java part is done, so let's add the loop inside the component. Open **BlogEntryIndexPage.wo** (it's located in the **Component** folder) and right after the <body> tag, add: 490 490 491 491 {{code}} 492 - 493 493 <wo:loop list="$entries" item="$entryItem"> 494 494 <p><wo:str value="$entryItem.title" /></p> 495 495 <p><wo:str value="$entryItem.author.fullName" /></p> ... ... @@ -506,7 +506,6 @@ 506 506 Open **BlogEntryShowPage.java** and make sure the class implements **er.rest.routes.IERXRouteComponent**. 507 507 508 508 {{code}} 509 - 510 510 import er.rest.routes.IERXRouteComponent; 511 511 512 512 public class BlogEntryShowPage extends WOComponent implements IERXRouteComponent { ... ... @@ -516,7 +516,6 @@ 516 516 We need to add other methods to receive the BlogEntry object from the controller. In **BlogEntryShowPage.java**, add: 517 517 518 518 {{code}} 519 - 520 520 private BlogEntry blogEntry; 521 521 522 522 @ERXRouteParameter ... ... @@ -535,7 +535,6 @@ 535 535 The Java part of the work is done, so save the Java class. It's time to work on the component part. Open **BlogEntryShowPage.wo** and between the <body></body> part, add: 536 536 537 537 {{code}} 538 - 539 539 <h1><wo:str value="$blogEntry.title" /></h1> 540 540 <p><wo:str value="$blogEntry.content" /></p> 541 541 <p>Created on: <wo:str value="$blogEntry.creationDate" dateformat="%Y/%m/%d" /></p> ... ... @@ -546,7 +546,6 @@ 546 546 Our view component is done, the only thing remaining is a link for the blog entry list (BlogEntryIndexPage) to the view page (BlogEntryShowPage). Save **BlogEntryShowPage.wo** and open **BlogEntryIndexPage.wo**. We are going to add a link on the title, you will replace to replace this: 547 547 548 548 {{code}} 549 - 550 550 <p><wo:str value="$entryItem.title" /></p> 551 551 552 552 {{/code}} ... ... @@ -554,7 +554,6 @@ 554 554 with: 555 555 556 556 {{code}} 557 - 558 558 <p><wo:ERXRouteLink entityName="BlogEntry" record="$entryItem" action="show"><wo:str value="$entryItem.title" /></wo:ERXRouteLink></p> 559 559 560 560 {{/code}}