Changes for page Your First Rest Project
Last modified by Steve Peery on 2013/09/06 11:02
From 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. filippolauria1 +XWiki.skcodes - Content
-
... ... @@ -1,5 +3,3 @@ 1 - 2 - 3 3 {{toc/}} 4 4 5 5 = Introduction = ... ... @@ -122,8 +122,6 @@ 122 122 123 123 The model should show up in a window that looks like this: 124 124 125 -[[image:attach:EOModeler.png]] 126 - 127 127 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. 128 128 129 129 In the Entity Modeler window, click on **Default**, and for the **URL** field, type ... ... @@ -231,6 +231,7 @@ 231 231 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: 232 232 233 233 {{code}} 230 + 234 234 public String fullName() { 235 235 return this.firstName() + " " + this.lastName(); 236 236 } ... ... @@ -240,6 +240,7 @@ 240 240 Nothing fancy here. Now open **BlogEntry.java** and add the following method: 241 241 242 242 {{code}} 240 + 243 243 @Override 244 244 public void awakeFromInsertion(EOEditingContext editingContext) { 245 245 super.awakeFromInsertion(editingContext); ... ... @@ -269,6 +269,7 @@ 269 269 Remove the pound char in front of those two properties: 270 270 271 271 {{code}} 270 + 272 272 #er.migration.migrateAtStartup=true 273 273 #er.migration.createTablesIfNecessary=true 274 274 ... ... @@ -277,6 +277,7 @@ 277 277 After removing the pound char, the two properties should look like this: 278 278 279 279 {{code}} 279 + 280 280 er.migration.migrateAtStartup=true 281 281 er.migration.createTablesIfNecessary=true 282 282 ... ... @@ -285,6 +285,7 @@ 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 + 288 288 BlogRest[62990] INFO er.extensions.migration.ERXMigrator - Upgrading BlogModel to version 0 with migration 'your.app.model.migrations.BlogModel0@4743bf3d' 289 289 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) 290 290 BlogRest[62990] INFO er.extensions.jdbc.ERXJDBCUtilities - Executing ALTER TABLE Author ADD PRIMARY KEY (id) ... ... @@ -340,6 +340,7 @@ 340 340 Add this method in **BlogEntryController**: 341 341 342 342 {{code}} 344 + 343 343 protected ERXKeyFilter filter() { 344 344 ERXKeyFilter personFilter = ERXKeyFilter.filterWithAttributes(); 345 345 personFilter.setAnonymousUpdateEnabled(true); ... ... @@ -356,6 +356,7 @@ 356 356 Now, let's implement the **createAction** method: 357 357 358 358 {{code}} 361 + 359 359 public WOActionResults createAction() throws Throwable { 360 360 BlogEntry entry = create(filter()); 361 361 editingContext().saveChanges(); ... ... @@ -369,6 +369,7 @@ 369 369 Last step in the controller: implementing the **indexAction** method. Again, the code is simple: 370 370 371 371 {{code}} 375 + 372 372 public WOActionResults indexAction() throws Throwable { 373 373 NSArray<BlogEntry> entries = BlogEntry.fetchAllBlogEntries(editingContext()); 374 374 return response(entries, filter()); ... ... @@ -385,6 +385,7 @@ 385 385 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: 386 386 387 387 {{code}} 392 + 388 388 ERXRouteRequestHandler restRequestHandler = new ERXRouteRequestHandler(); 389 389 restRequestHandler.addDefaultRoutes(BlogEntry.ENTITY_NAME); 390 390 ERXRouteRequestHandler.register(restRequestHandler); ... ... @@ -394,7 +394,7 @@ 394 394 395 395 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. 396 396 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"]] 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"]] 398 398 399 399 == Adding posts and authors with curl == 400 400 ... ... @@ -409,6 +409,7 @@ 409 409 The response should look this: 410 410 411 411 {{code}} 417 + 412 412 HTTP/1.0 201 Apple WebObjects 413 413 Content-Length: 249 414 414 x-webobjects-loadaverage: 0 ... ... @@ -421,6 +421,7 @@ 421 421 To get a list of blog entries: 422 422 423 423 {{code}} 430 + 424 424 curl -X GET http://192.168.0.102:52406/cgi-bin/WebObjects/BlogRest.woa/ra/blogEntries.json 425 425 426 426 {{/code}} ... ... @@ -432,6 +432,7 @@ 432 432 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: 433 433 434 434 {{code}} 442 + 435 435 @Override 436 436 protected boolean isAutomaticHtmlRoutingEnabled() { 437 437 return true; ... ... @@ -444,6 +444,7 @@ 444 444 The next step to get it to work is to make **BlogEntryIndexPage** to implement the **er.rest.routes.IERXRouteComponent** interface. 445 445 446 446 {{code}} 455 + 447 447 import er.rest.routes.IERXRouteComponent; 448 448 449 449 public class BlogEntryIndexPage extends WOComponent implements IERXRouteComponent { ... ... @@ -453,6 +453,7 @@ 453 453 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: 454 454 455 455 {{code}} 465 + 456 456 public NSArray<BlogEntry> entries() { 457 457 EOEditingContext ec = ERXEC.newEditingContext(); 458 458 return BlogEntry.fetchAllBlogEntries(ec, BlogEntry.CREATION_DATE.descs()); ... ... @@ -463,6 +463,7 @@ 463 463 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**: 464 464 465 465 {{code}} 476 + 466 466 private BlogEntry entryItem; 467 467 468 468 public BlogEntry entryItem() { ... ... @@ -478,6 +478,7 @@ 478 478 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: 479 479 480 480 {{code}} 492 + 481 481 <wo:loop list="$entries" item="$entryItem"> 482 482 <p><wo:str value="$entryItem.title" /></p> 483 483 <p><wo:str value="$entryItem.author.fullName" /></p> ... ... @@ -494,6 +494,7 @@ 494 494 Open **BlogEntryShowPage.java** and make sure the class implements **er.rest.routes.IERXRouteComponent**. 495 495 496 496 {{code}} 509 + 497 497 import er.rest.routes.IERXRouteComponent; 498 498 499 499 public class BlogEntryShowPage extends WOComponent implements IERXRouteComponent { ... ... @@ -503,6 +503,7 @@ 503 503 We need to add other methods to receive the BlogEntry object from the controller. In **BlogEntryShowPage.java**, add: 504 504 505 505 {{code}} 519 + 506 506 private BlogEntry blogEntry; 507 507 508 508 @ERXRouteParameter ... ... @@ -521,6 +521,7 @@ 521 521 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: 522 522 523 523 {{code}} 538 + 524 524 <h1><wo:str value="$blogEntry.title" /></h1> 525 525 <p><wo:str value="$blogEntry.content" /></p> 526 526 <p>Created on: <wo:str value="$blogEntry.creationDate" dateformat="%Y/%m/%d" /></p> ... ... @@ -531,6 +531,7 @@ 531 531 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: 532 532 533 533 {{code}} 549 + 534 534 <p><wo:str value="$entryItem.title" /></p> 535 535 536 536 {{/code}} ... ... @@ -538,6 +538,7 @@ 538 538 with: 539 539 540 540 {{code}} 557 + 541 541 <p><wo:ERXRouteLink entityName="BlogEntry" record="$entryItem" action="show"><wo:str value="$entryItem.title" /></wo:ERXRouteLink></p> 542 542 543 543 {{/code}}