Using BlazeDS to Send User-Defined Data Types (Data Transfer Objects) from Java to Flex


This is part 3 of my series on how to use BlazeDS to enable Flex to communicate with Java on the backend. If you've not yet read parts 1 and 2, you should review them before continuing. In this blog entry I'll explain how I created an example to have Java send a user-defined data type (a data transfer object) and a collection of user-defined data types to the Flex application.

How To Setup This Example

View this example online and right click on the application to get the source code.

In Java create this Person class.

package example;

public class Person {
    private String lastName;
    private String phone;
    public Person() {
    public Person(String aLastName, String aPhone) {
        lastName = aLastName;
        phone = aPhone;
    public String getLastName() {
        return lastName;

    public void setLastName(String lastName) {
        this.lastName = lastName;

    public String getPhone() {
        return phone;

    public void setPhone(String phone) { = phone;

public String toString() {
    String tempStr;
    tempStr = "Last name: " + getLastName();
    tempStr = tempStr + "\nPhone: " + getPhone();
    return tempStr;


Then create a PersonService class in Java. This class will be the one that the Flex application will communicate with.

package example;

public class PersonService {
    public PersonService() {
    public Person getPerson() {
        Person aPerson = new Person("Phillips", "913-906-6001");
        return aPerson;
    public Person [] getPersons() {
        Person [] persons = new Person[2];
        Person person1 = new Person("Phillips", "913-906-6001");
        Person person2 = new Person("Smith", "713-341-3401");
        persons[0] = person1;
        persons[1] = person2;
        return persons;


Note the method getPerson returns a Person object and the method getPersons returns an Array of Person objects.

Compile both classes and copy them to the [tomcat-home]\webapps\blazeds\WEB-INF\classes\example folder (since both classes are in the package example).

Add the following destination node to remote-config.xml ([tomcat-home]\webapps\blazeds\WEB-INF\flex).

<destination id="personservice">
<adapter ref="java-object"/>

Note the id value is personservice. That will be the value we will use when setting up the mx:RemoteObject tag in Flex.

Stop Tomcat if its running and restart it.

Create a new Flex project following the guidelines given in the first tutorial.

Create a new ActionScript class named Person in the default package. Below is the code for that class.


public class Person

public var lastName:String = "";
public var phone:String = "";

public function Person()


Note that the alias value of the RemoteClass command is equal to the path to the Person class that is under [tomcat-home]\webapps\blazeds\WEB-INF\classes. Setting that command will enable Flex to automatically map the object returned by the Java class as type Person instead of a generic object.

Below is the code that goes between the mx:Application tags for the mxml file.


import mx.utils.ObjectUtil;
import mx.controls.Alert;
import mx.utils.StringUtil;

/*ensure our class is
compiled into the SWF
otherwise the array of Person objects
returned by the Java class will
be treated as generic objects

private var aPerson:Person ;

/*Called automatically when the result is returned by the
Java class method getPerson

public function resultGetPerson(event:ResultEvent):void {

//used for debugging - shows details about result
//returned by the CFC method ObjectUtil.toString(event.result) );

} //end function resultGetPerson

automatically called if the Java class method call causes an error

private function faultGetPerson(event:FaultEvent):void{ ObjectUtil.toString(event.fault) );

}//end function faultGetPerson

/*Called automatically when the result is returned by the
Java class method getPersons

public function resultGetPersons(event:ResultEvent):void {

//used for debugging - shows details about result
//returned by the CFC method ObjectUtil.toString(event.result) );

} //end function resultGetPersons

automatically called if the Java class method call causes an error

private function faultGetPersons(event:FaultEvent):void{ ObjectUtil.toString(event.fault) );

}//end function faultGetPersons


<!--setup the connection to the Java class-->

<mx:method name="getPerson"

<mx:method name="getPersons"


<mx:Panel x="10" y="10" width="400" height="250" layout="vertical" title="Test of Sending Complex Data Type From Java to Flex" borderColor="#008040" fontFamily="Arial" fontWeight="bold" fontSize="13">
<mx:Text text="Click A Button To Test The Java To Flex" fontWeight="bold"/>
<mx:Spacer height="20"/>

<mx:Button label="Get One Person Object" click="PersonService.getPerson()"/>

<mx:Spacer height="20" />

<mx:Button x="28" y="40" label="Get An Array of Person Objects" click="PersonService.getPersons()"/>


Note the comment in the mx:Script code block about declaring an Object of type Person.

Run the Flex application. If you click on the Get One Person Object button an alert box should pop up with the details for one Person object. Flex should correctly identify the object as type Person. If you click on the Get An Array of Person Objects button, an alert box should pop up with two Person objects contained in an Array. Again, Flex should correctly identify both objects in the Array as type Person.


Wow! That was easy. I really only had to change the backend from ColdFusion components to Java classes using a previous Flex-to-ColdFusion example I did last year.

I have to say, I'm starting to like using BlazeDS to enable Flex to work with Java on the backend. I need to explore further how to setup a Flex and Java application with BlazeDS in a production environment vs. my development environment. This post about Useful Patterns for BlazeDS might be helpful for that.

What's Next?

Let's get a few hundred records out of a database, iterate over the ResultSet, create an ArrayList containing a Person object for each record, and return the ArrayList to Flex where we will display the Person objects in a DataGrid component.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Hey Bruce,

Your timing could not be better. I am literally starting to do the exact same thing 1 day after this post, which is extremely helpful. Good work.

# Posted By Derrick Grigg | 6/24/08 8:59 AM
I'm looking forward to seeing your next post. I just started working on this today.
# Posted By Brad | 6/24/08 10:14 AM

Thanks for these tips! I am just starting a project using Flex and all this information is very useful right now!

Can't wait to see the next one getting hundreds of records from a DB ;-)
# Posted By Ricardo | 6/24/08 9:56 PM
I love You!
# Posted By Rodrigo Costa | 6/30/08 4:39 AM
Bruce, are you going to write some about Cairngorm?

big HUG!
# Posted By matias | 10/2/08 9:09 PM
BlogCFC was created by Raymond Camden. This blog is running version Contact Blog Owner