Exception Handling using WSDL Faults

This sample demonstrates how to specify a WSDL fault in order to allow your service to communicate exception pathways to your clients.

Running of this sample assumes that you are running this within the extracted release folder.

Constructing the Service and 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.

Using a command prompt, got to the folder of this example and type ant generate.service or just ant. This will:

Open up service/src/example/BankServiceSkeleton.java and you will see the following code fragment inside #withdraw method. When generate.service has been executed, Axis2 generates a skeleton class (build/service/src/example/BankServiceSkeleton.java) which has an empty method. We replace it, the genedrated skeleton with a pre-populated skeleton class (service/src/example/BankServiceSkeleton.java) which has the following code inside it.

 final String account = param0.getAccount();
    if (account.equals("13")) {
    final AccountNotExistFault fault = new AccountNotExistFault();
    fault.setAccount(account);
    AccountNotExistFaultMessageException messageException = new
    AccountNotExistFaultMessageException("Account does
    not exist!");
    messageException.setFaultMessage(fault);
    throw messageException;
    }

    final int amount = param0.getAmount();
    if (amount > 1000) {
    final InsufficientFundFault fault = new InsufficientFundFault();
    fault.setAccount(account);
    fault.setBalance(1000);
    fault.setRequestedFund(amount);
    InsufficientFundFaultMessageException messageException = new
    InsufficientFundFaultMessageException("Insufficient
    funds");
    messageException.setFaultMessage(fault);
    throw messageException;
    }

    final WithdrawResponse response = new WithdrawResponse();
    response.setBalance(1000 - amount);
    return response;

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

Deploying the Service

The above step must have already copied your BankService.aar file in to repository/services/ folder. Then go to bin folder and run either of axis2server.bat or axis2server.sh, depending on your platform in order to startup Axis2 server.

With the default configuration, if you go to http://localhost:8080/axis2/ you should see BankService was deployed.

Running the Client

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

When you call ant run.client with parameters, before running client/src/example/BankClient.java class, it does the following as well: