Exception Handling using WSDL Faults

This example show cases how to specify a WSDL fault in order to allow your service to communicate exception pathways to your clients.

Before you start you will need to set the AXIS2_HOME environment variable.

Constructing the Service and the Client.

The first step is to generate the service skeleton and other interface classes from the WSDL.

Look at bank.wsdl. It defines the interface for our service. Of particular interest are the AccountNotExistFault and InsufficientFundFault types defined in the wsdl:types element.

From a command prompt in the folder of this example, type ant generate.service. This will:

Open up ./service/target/src/example/BankServiceSkeleton.java and insert the code below into the #withdraw method.

	final String account = param0.getAccount();
	if (account.equals("13")) {
		final AccountNotExistFault fault = new AccountNotExistFault();
		fault.setAccount(account);
		throw new AccountNotExistFaultMessageException("Account does not exist!", fault);
	}
	
	final int amount = param0.getAmount();
	if (amount > 1000) {
		final InsufficientFundFault fault = new InsufficientFundFault();
			fault.setAccount(account);
			fault.setBalance(1000);
			fault.setRequestedFund(amount);
		throw new InsufficientFundFaultMessageException("Insufficient funds", fault);
	}
	
	final WithdrawResponse response = new WithdrawResponse();
	response.setBalance(1000 - amount);
	return response;
		

From a command prompt in the folder of this example, type ant jar. This will and create the service archive at ./service/target/build/lib/BankService.aar

Note that the source generated for the client will include the 2 faults and the local Exceptions through which they will be transmitted. Note that the Exceptions are generated within the BankStub class.

Running the Client.

Deploy the service archive to the Axis2 web application.

Invoke the example.BankClient class. You may use the command scripts to do so. You need to supply 3 parameters to the command, url, account and amount.