Getting started with Adobe Flex and Axis2 ( part 4 : Testing the WebService ) 0

Before connecting the Flex client to the web service we want to test it. We will test the web service using the SOAP ui tool, you should have downloaded it in part 1 of this tutorial.

Start SOAPui using the start.bat/sh file in the bin directory of the distribution.

Create the test project

Create a new WSDL project and enter the WSDL location ( http://localhost:8080/axis2/services/FlexService?wsdl ) at the Initial WSDL field. The project name can be anything you like, i used FlexService.

Perform some test operations

Open the FlexServiceSOAP12Binding tree, and open the operations. Since you already know what the operations should do you can send some test requests.

Send some test requests to all operations, make sure they all return valid SOAP. You should be able to get a list of products, update a price/description, delete a product and add a new product.

If everything is working as expected we’re done with the Java part of the application. We can now build the Flex application and consume this web service in a RIA.

Getting started with Adobe Flex and Axis2 ( part 3 : Deploying the WebService ) . 0

In the previous part we wrote the neccesary Java code, in this part we will focus on packaging the Java code into a .aar file.

Create directory structure

The first step is to setup the required directories in the root of the Eclipse project, create all directories described below:

  • dist
    This directory contains the files generated by Ant, it will only contain FlexService.aar
  • output
    This directory contains the directory structure which will be used by Ant to create the aar file
  • resources/META-INF/
    This directory contains the static XML file(s) which will be copied to the output directory.

Create the Axis2 configuration file

The only file we need to deploy the POJO we created before is services.xml.

  • Click here to download services.xml, store this file in the resources/META-INF/ directory.

The next file is the ant build file:

  • Click here to download build.xml file, store this file in the project root

Open the build.xml file in Eclipse and make sure the properties deploy.dir and axis.dir contains valid values.

At this point you should be able to open the build.xml file in Eclipse and run the compile and aar targets. Please do so now, and check the console for any messages.

the console should read something like this for the compile target:

Buildfile: C:\java\workspace\FlexService\build.xml
compile:
BUILD SUCCESSFUL
Total time: 4 seconds

the aar target should ouput something like this:

Buildfile: C:\java\workspace\FlexService\build.xml
compile:
aar:
[delete] Deleting directory C:\java\workspace\FlexService\output\aar\META-INF
[mkdir] Created dir: C:\java\workspace\FlexService\output\aar\META-INF
[copy] Copying 1 file to C:\java\workspace\FlexService\output\aar\META-INF
[jar] Building jar: C:\java\workspace\FlexService\dist\FlexService.aar
BUILD SUCCESSFUL
Total time: 1 second

We now have a (hopefully) valid .aar file, which we can deploy to axis ( which is running within tomcat ).
Try deploying the file to axis using the deploy build target, this will perform the previous steps again but that’s ok.
The deploy task should output something like this:

Buildfile: C:\java\workspace\FlexService\build.xml
compile:
aar:
[delete] Deleting directory C:\java\workspace\FlexService\output\aar\META-INF
[mkdir] Created dir: C:\java\workspace\FlexService\output\aar\META-INF
[copy] Copying 1 file to C:\java\workspace\FlexService\output\aar\META-INF
[jar] Building jar: C:\java\workspace\FlexService\dist\FlexService.aar
deploy:
[copy] Copying 1 file to C:\java\libs\apache-tomcat-6.0.14\webapps\axis2\WEB-INF\services
BUILD SUCCESSFUL
Total time: 780 milliseconds

If this works out ok it’s time to take a look at your tomcat console (or start tomcat if it isn’t running).
The following line should appear in the output :

[INFO] Deploying Web Service: FlexService.aar

This means Axis succesfully deployed the web service, to double check this try to display the WSDL file:

http://localhost:8080/axis2/services/FlexService?wsdl

A whole bunch of XML should appear at this URL.

Proceed to part 4

 

 

 

Getting started with Adobe Flex and Axis2 ( part 2 : Building the WebService ) 0

Create the Eclipse project

The first step in building the web service is creating the Eclipse project:

  • Create a default Java Project
    • Name the project FlexService ( or whatever you like.. :) )
    • In the third window of the Project click the libraries tab, use the “Add external jar” button to add the following files from the axis2 distribution directory ( located in /lib/ ).
      • axis2-kernel-1.3.jar
    • click Finish to create the project

Create the Java classes

Create the following packages within the src/ directory

    • com.test.service
    • com.test.model

Create the model

Create the Java class : com.test.model.Product
this class represents a simple product, create the following field and the appropriate getter and setter methods :

private BigDecimal price;
private String description;
private Integer id;

Add the following code to the product class, it contains some easy constructors and a toString method.

public Product(){
super();
}
public Product( Integer id, BigDecimal price, String description) {
super();
this.id = id;
this.price = price;
this.description = description;
}
public Product( BigDecimal price, String description) {
super();
this.price = price;
this.description = description;
}
public String toString(){
return id+”:”+description+” -> “+price;
}

Create the endpoint

The next Java file is Axis endpoint, it is a simple POJO which allows us to access the Products HashMap using SOAP.

Create the Java class : com.test.service.FlexService

Add the following fields

private Map<Integer, Product> products = new HashMap<Integer, Product>();
private int idCounter = 0;

The HashMap contains a Mapping of Integers ( the product ID’s ) to Products. Normally you would use a database for this kind of data, but since the tutorial is about SOAP and Flex i chose for the simplest thing that worked :) .

The following constructor will create an initial list of products, containing some simple example values.
The idCounter field is used as identified for the products.

public FlexService(){
products.put(++idCounter,new Product(idCounter,BigDecimal.valueOf(12.50),”TV”));
products.put(++idCounter,new Product(idCounter,BigDecimal.valueOf(22.50),”Radio”));
products.put(++idCounter,new Product(idCounter,BigDecimal.valueOf(32.50),”Wii”));
products.put(++idCounter,new Product(idCounter,BigDecimal.valueOf(42.50),”Xbox 360″));
products.put(++idCounter,new Product(idCounter,BigDecimal.valueOf(52.50),”Playstation 3″));
}

The following method will return a simple array of products. It takes the values from the HashMap using the values() methods.

public Product[] getProducts(){
return products.values().toArray(new Product[0]);
}

The following methods adds a Product to the products HashMap, again the idCounter is used to determine the productId.

public Integer addProduct(String description, BigDecimal price){
Product p = new Product(++idCounter,price,description);
products.put(idCounter,p);
return idCounter;
}

The following method removes a product from the HashMap, the method returns true or false depending on the existence of the product.

public boolean removeProduct(Integer id){
System.out.println(“Removing product id : “+id);
if ( products.containsKey(id)){
products.remove(id);
return true;
} else {
return false;
}
}

The last methods updates the price and description of a given product id, it returns a boolean indicating the succes of the operation.

public boolean updateProduct(Integer id, String description, BigDecimal price){
System.out.println(“Updating product : “+products.get(id));
if ( products.containsKey(id)){
Product currentProduct = products.get(id);
currentProduct.setPrice(price);
currentProduct.setDescription(description);
return true;
} else {
System.out.println(“Product id not found : “+id);
return false;
}
}

That’s it for the Java code, the next part will explain the deployment of this service to axis.

Proceed to step 2

Getting started with Adobe Flex and Axis2 ( part 1 : Preparation ) 0

Introduction

This tutorial will help you to get started with Adobe Flex and Apache Axis2. We will build a very simple product management application. There is no databases involved, everything is stored in a simple HashMap to keep it simple.
This tutorial is divided into five parts :

  • Part 1 : Preparations ( this page )
  • Part 2 : Build the web service
  • Part 3 : Deploy the web service
  • Part 4 : Test the web service
  • Part 5 : Getting started with Flex

Software requirements
In order to complete this tutorial you will need the following applications / libraries:

  • A recent JDK
  • Eclipse ( i used 3.3.1.1 )
  • Apache Axis2 ( i used 1.3, thumbs up for whoever decided Axis2 versioning should start at 0 instead of 2…)
  • Apache Tomcat 6 ( i used 6.0.14 )
  • Apache Ant ( any version will do )
  • Adobe Flex ( i used 2.0.1, 3.0 was released on the day i wrote this, seems to work to :) )
  • SOAP UI ( just the latest version, this is just for debugging )

Download all required tools or make sure you have extracted them somewhere on your system.

Preparing Axis

In order to deploy web services to tomcat we need to deploy Axis2 into Tomcat. Please check the file installation-std-bin.txt in the axis2 distribution for more details about this step. Basically you should perform the following steps:

  • Type ant in the webapp directory
  • Copy the axis2.war file from the dist/ directory the the webapps directory of tomcat.

The following lines should appear in the tomcat log files after starting tomcat.

INFO: Deploying web application archive axis2.war
[INFO] Deploying module: addressing-1.3
[INFO] Deploying module: script-1.3

At this point your tomcat installation is ready to handle SOAP requests. The next part of this tutorial will focus on building a simple web service and deploying it to your tomcat installation.

Open / Close

Blogroll

Your List

  • Your list items
  • Your list items
  • Your list items
  • Your list items
  • Your list items