. .

tutorial

RESTful Services in Seaside

February 2, 2010 8:19:03.557

Over the weekend, I demonstrated a RESTful JSON service using the WebToolkit. Today, I'm going to do the same thing using Seaside 3.0 in VisualWorks (the same code should work in ObjectStudio). If you want the code, here it is. First, load Seaside from the Parcel Manager - it's now under "Web Development":

The example I'm building uses JSON (although it could just as easily be XML or anything else) - so we'll connect to the public store and get the JSON code support:

Next, we'll create a subclass of WAComponent that will render the JSON for us. As I did over the weekend, I'll just create a simple dictionary for the example:

Next, we need to write some code. Add the following two class methods:


canBeRoot
	^true



initialize
	"MyService initialize"

	self registerAsApplication: 'SvcPoint'.

In a workspace, execute MyService initialize. At this point, you have a working service, it just doesn't do anything. Go to the launcher and pull down the Seaside menu and start the server. If you go to SvcPoint, you should get a blank page.

Now, you need to create the rendering code to respond with JSON. This is a bit different than it would have been in Seaside 2.8 - the session and request context code have changed.

Write the code above:


renderContentOn: html
	" Render our object as generic json. "


	self requestContext respond: [:response | 
		| json |
		json := self data asJson.
		response contentType: 'application/json'.
		response nextPutAll: json ]

Now browse the entry point again - you should be prompted to download a file (or it'll just download, depending on the browser you're using):

Finally, if you open that response file in a text editor, you should see this:

And that's it! You now have a RESTful service implemented in Seaside. Here's the code in a zip file if you just want to load it and take a look.

Technorati Tags: , , ,

posted by James Robertson

 Share Tweet This