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/ ).
- 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