Flex and Spring: Communication over BlazeDS – Part I

Leveraging a Spring backend in your Flex application can be a great way to gain access to powerful java tools and server technologies, but getting your client and server all wired up can be pretty challenging, especially for the newcomer. I recently had the opportunity to explore this Pandora’s box, and came up with a Flex-Java solution that may work for you, depending on your project’s needs. In this series of tutorials I want to share with you the way in which I went about getting my application up and running in hopes that other newbies out there might have some success integrating these technologies.

NOTE: Please download attached source files from here to help you with this tutorial.

I wanted to make use of the Spring framework in my Java backend because of its flexibility and streamlined development. I wanted Hibernate integration also for data persistence to a MySQL database, which I knew Spring could provide me. It seemed to me that this stack would be the perfect combination of tools to get my application off the ground, but I wasn’t quite sure how to go about putting all these pieces together.

A little research on this topic revealed some solutions like Spring Roo with the Flex scaffolding extensions which, at least at first, seemed would glue all these components together with ease. I’m sure the Spring Roo project, once it matures a little and stabilizes, will be a viable way to generate the Java classes and Flex remoting scaffold necessary to build an application. That being said, the amount of difficulty I had in getting Spring Roo and Flex to work together was immense. I also have a huge aversion to using new tools to generate code for me that are not yet widely embraced or supported by the community at large, and which I don’t fully understand inside and out. When eventually the excrement hits the air circulation device, I want to know exactly where a system is failing and why, even if it means spending a few extra hours writing boilerplate code from scratch. I do, however, appreciate the work that Jeremy Grelle et al. have done on this project, but at present, the Roo-Flex project isn’t stable enough for my tastes. Upon realizing this, I decided to roll up my sleeves and tie the pieces together myself.

Setting up BlazeDS and Tomcat

BlazeDS is a great way to get Flex and java talking to one another. It is free, which lowers the cost barrier for you and your client, and is relatively easy to install. A popular method of getting up and running quickly is to install the BlazeDS turnkey server. This is ostensibly a Tomcat server, bundled with a bunch of very typical examples of how BlazeDS can be utilized. You can download this package from the Adobe Labs Website, or if you already have a Tomcat installation you want to develop against, you may grab the WAR file and deploy it yourself. Instructions for deploying BlazeDS, starting the server and running the examples can all be found on the website, and in the enclosed documentation.

Maven Management

Once you have a Tomcat installation and BlazeDS running in the container, we can start thinking about how to structure our java project and how to incorporate the Spring and Hibernate libraries into our project. I found SpringSource Tool Suite (STS) to be a useful IDE to develop the java side of my application. It is another Eclipse spinoff, so those familiar with this IDE will feel right at home in these environs. If you use Maven to manage your project dependencies, you can get almost everything you need by adding the requisite nodes to your pom.xml file, or by using the M2Eclipse pom editor to search for and manage your project jars. You can find instructions on installing M2Eclipse for STS on the M2Eclipse project website, and you can get SpringSource Tool Suite from here.

Once we have these pieces in place, let’s start by making a new project in STS. Choose ‘New Java Project’ from ‘File’ in the main menu of the application, and give it a name. This is the most basic java project configuration, to which we are going to add Maven support, as well as some project facets to get the resources and structure of our project tightened up. The next thing we want to do is enable dependency management with Maven. We’ll do that by right clicking on the project in our package explorer, and choosing ‘Maven -> Enable Dependency Management’. Here we are presented with some options. Because we are just creating a simple tutorial application here, I’m leaving my Group Id and Artifact Id alone, and providing a simple name and description for the heck of it. When finished with this step, a pom.xml file will be created in the project folder which we can edit to add dependencies automatically to the build path.

Project Facets

The way Eclipse organizes a project and structures a project to make it runnable in different environments is to add ‘facets’ to the project. We are going to add facets to this project now to transform this ‘vanilla’ java project into web application deployable to our Tomcat server. Again, right click on the project folder in the Package Explorer and choose ‘Properties’. You’ll see a menu item in the list called ‘Project Facets’. Choose that, and click the link to ‘Convert to faceted form…’. From the list that appears in the Project Facets Pane, select the ‘Dynamic Web Module’ checkbox, leave the ‘Java’ checkbox selected and click ‘OK’.

If you were paying attention you’ll notice that Eclipse just created a folder called ‘WebContent’, and  ‘META-INF’ and ‘WEB-INF’ folders inside that folder. This is the basic structure our java web application will take on when we export and deploy it.

Spring Dependencies

The Maven dependencies found in the attached project’s pom.xml file will give you the libraries we’re going to make use of in the next step of our tutorial. Note that there may be updates to some of these release jars by now, but avoiding the confusion of dependency compatibility is one of my goals in writing this post. Try using this list of dependencies before looking for newer releases, and save yourself some aggravation.

Feel free to copy this list directly into your project’s pom. Once we have done this, we are ready to begin building our java application’s context. You should also see that in the package explorer, our folder containing Maven Dependencies has 17 new jar files in it. These are, of course, the dependencies we’ve just added to our pom.xml.

Spring Flex Integration Jars

In addition to the above resources, we’re going to need the Flex Spring integration jars. You can get them from the following address.

Once you’ve downloaded these jars, you’ll want to make sure they get deployed along with your maven managed jars. I made a folder inside my project called ‘jar’ into which I’ve placed additional dependencies. I’ve included my jars folder here in the project archive, so you can unzip that folder into your project directory and use them. There are a few other jars in this folder. Do yourself a favor and use my jars because there are some dependencies that we have not discussed, which I spent too many hours starting and restarting my server, getting ClassDefinitionNotFound errors, and searching the web for the correct jars containing the right Classes to get this project running. Please trust me on this one. To get the items in the ‘jars’ folder to deploy properly, you want to make sure there is a corresponding entry in your ‘Web Deployment Assembly’ properties for this project. Go into the Properties for this project and select Deployment Assembly. This is where we can define mappings for deployment of our resources. If it isn’t done already, map your jar folder to ‘/WEB-INF/lib’. Our jars will now be in place when we deploy our web application.

Part I Wrap Up

Because this process of building all the components that make up this application is quite involved, I am breaking this into smaller, less daunting steps. We now have an STS project with all of the foundation to start coding our backend, and after that, to call our services from a Flex client. Be sure to check back soon for the second part of this tutorial, when we get into some of the more fun aspects of building this project.

About Curious Minds
We are a web development firm in New York and Chicago, providing development resources and consulting for websites and mobile apps since 2004.