Sending ActionScript objects through BlazeDS with ASObject

Source code (LICENSE)

Typically, BlazeDS is used to send typed objects between a Flex client and a Java server, but what if you want to send generic objects generated dynamically? On the client side, you can create an ActionScript object dynamically through the Object base class. So, for example, you can write something like this:

var myObject: Object = new Object();
myObject.prop1 = "prop1Value";
myObject.prop2 = new Object();
myObject.prop2.prop21 = "subValue1";
myObject.prop2.prop22 = "subValue2";

Just a simple example that shows how to dynamically add custom properties to a generic ActionScript Object. What could not be obvious is how to obtain the same functionality on the server side with Java. In fact, the Object class in ActionScript corresponds to the ASObject class in Java. What is important to note is that the ASObject class extends java.util.HashMap. How can this information be useful? To understand it, let’s immediately take a look at our example application.

<mx:Application xmlns:mx=""
	pageTitle="Sending ActionScript objects through BlazeDS with ASObject - devahead BLOG"
	layout="vertical" horizontalAlign="left">
			import mx.controls.Alert;
			import mx.rpc.AsyncToken;
			protected static const ENDPOINT: String = "messagebroker/amf";
			protected function getRemoteObject(): void
				// Create a generic ActionScript object to send to the server
				var sentGenericObj: Object = new Object();
				sentGenericObj.prop1 = 123;
				sentGenericObj.composite = new Object();
				sentGenericObj.composite.sub1 = "subValue1";
				sentGenericObj.composite.sub2 = 0.57;

				var call: AsyncToken = testService.getSentGenericObject(sentGenericObj);
				call.addResponder(new mx.rpc.Responder(
					onGetRemoteObjectOK, onGetRemoteObjectFault));
			protected function onGetRemoteObjectOK(event: ResultEvent): void
				var result: Object = event.result;"Returned object:\n\n" +
					"prop1 = " + result.prop1 + "\n" +
					"composite.sub1 = " + result.composite.sub1 + "\n" +
					"composite.sub2 = " + result.composite.sub2);

			protected function onGetRemoteObjectFault(event: FaultEvent): void
			{"Error retrieving remote object.\n\n" +
					"faultString: " + event.fault.faultString + "\n" +
					"faultDetail: " + event.fault.faultDetail + "\n" +
					"faultCode: " + event.fault.faultCode, "Error");

	<mx:RemoteObject id="testService" destination="testService" endpoint="{ENDPOINT}"
	<mx:Button label="Get remote object" click="{getRemoteObject()}"/>

In this application, we call a server function named getSentGenericObject. It’s purpose is simply to send back to the client an object generated dynamically on the server side whose values are the same of the object received in input from the client. As you can see, we create a generic ActionScript object called sentGenericObj, add some properties and send it to the server, then we print on an alert message the values of the object received from the server. Now it’s time to see how getSentGenericObject is implemented.



public class TestService
	public Object getSentGenericObject(ASObject genericObj)
		// Create a new ASObject instance to send to the client with all
		// the properties values received from the client.
		ASObject returnedObj = new ASObject();
		returnedObj.put("prop1", genericObj.get("prop1"));
		ASObject compositeObj = new ASObject();
		returnedObj.put("composite", compositeObj);
		compositeObj.put("sub1", ((ASObject)genericObj.get("composite")).get("sub1"));
		compositeObj.put("sub2", ((ASObject)genericObj.get("composite")).get("sub2"));
		return returnedObj;

Since ASObject is in fact an HashMap, all we need to do to generate dynamically an object in Java is add the properties we need as keys of the HashMap and for each property we can assign a value. It’s really simple. As you can see, we create a new instance of ASObject called returnedObj and add a property named prop1 getting its value from the genericObj received from the client. We also create a composite property called composite. With composite I mean a property with sub-properties in it, so composite is another ASObject instance and we can add the sub-properties as we previously did with prop1. This is all we need to do. In this way, we can get a dynamically generated object both on the client and the server side and send it back and forth through BlazeDS. This is the result we get when we click the button on the example application:

Leave a comment