| Software | Version Required |
|---|---|
| WSO2 Developer Studio | 2.0.0 |
| Java Development Kit (JDK) | JDK 6 Update 32 |
| WSO2 Application Server | 4.5.0 M3 |
| Apache CXF | 2.5.4 |
First, we create an empty JAX-WS service Project, to create a JAX-WS Service Project,
public int createAccount(boolean print); public boolean checkAccount(int accountNo); public int checkAccountBalance(int accountNo); public boolean creditAccount(int accountNo, int amount); public boolean debitAccount(int accountNo, int amount);
package org.example.webservice;
import javax.jws.WebService;
@WebService
public interface AccountService {
public int createAccount(boolean print);
public boolean checkAccount(int accountNo);
public int checkAccountBalance(int accountNo);
public boolean creditAccount(int accountNo, int amount);
public boolean debitAccount(int accountNo, int amount);
}
package org.example.webservice;
import java.util.Hashtable;
import java.util.Random;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(serviceName = "AccountServiceImpl")
public class AccountServiceImpl implements AccountService {
// Account no list of the customers
static Hashtable<integer, integer> accountStore = new Hashtable<integer, integer>();
/**
* Generates the random account no at registration
*
* @return the random account number integer
*/
private int randomAccountGenerate() {
Random randomGenerator = new Random();
return randomGenerator.nextInt(1000);
}
/**
* create a new account and store
*
* @return accountNo
*
*/
@WebMethod(operationName = "createAccount")
public int createAccount(@WebParam(name = "printOnConsole") boolean print) {
int accountNo = 0;
// Generate the random account no
accountNo = randomAccountGenerate();
// create the entry in the account store
accountStore.put(accountNo, 0);
if(print){
System.out.println("New account created, No= " + accountNo);
}
return accountNo;
}
/**
* Check whether the account is available
*
* @param accountNo
* @return isAvailable
*
*/
@WebMethod(operationName = "checkAccount")
public boolean checkAccount(@WebParam(name = "accountNo") int accountNo) {
if (accountStore.containsKey(accountNo)) {
return true;
}
return false;
}
/**
* @param accountNo
* @return balance
*/
@WebMethod(operationName = "checkAccountBalance")
public int checkAccountBalance(@WebParam(name = "accountNo") int accountNo) {
if (checkAccount(accountNo)) {
return accountStore.get(accountNo);
}
return 0;
}
/**
* Increase the balance by credit amount
*
* @param accountNo
* @param amount
* @return success
*
*/
@WebMethod(operationName = "creditAccount")
public boolean creditAccount(@WebParam(name = "accountNo") int accountNo,
@WebParam(name = "amount") int amount) {
if (checkAccount(accountNo)) {
accountStore.put(accountNo,
(accountStore.remove(accountNo) + amount));
return true;
}
return false;
}
/**
* Decrease the balance by the debit amount
*
* @param accountNo
* @param amount
* @return Operation successfulness
*
*/
@WebMethod(operationName = "debitAccount")
public boolean debitAccount(@WebParam(name = "accountNo") int accountNo,
@WebParam(name = "amount") int amount) {
if (checkAccount(accountNo) && (accountStore.get(accountNo) >= amount)) {
accountStore.put(accountNo,
(accountStore.remove(accountNo) - amount));
return true;
}
return false;
}
}
There are two method to deploy our JAX-WS service into WSO2 Application Server.
We can export JAX-WS project as a web application (war) file and deploy it on WSO2 Appliation Server via web console.
To create a deployable archive (WAR) and deploy on WSO2 Application Server
We can add it as dependency of Carbon Application project which can deploy on WSO2 Application Server.
To do this,
We can test our web service using the WSO2 Application Server's web console
Log into WSO2 Application Server console, in the navigator, under Manage/JAX-WS/JAX-RS, click List. The Running JAX-WS/JAX-RS Applications page appears.

Click on "Find Services", It will list all SOAP and RESTful services deployed through our web service project.

We can copy WSDL's of the services (e.g. http://10.100.3.42:9763/AccountServiceProject-1.0.0/services/account_service?wsdl) and try those services using the external Try-It tool which can be found under Tools/Try-It menu of WSO2 Application server.

We can try out "createAccount" method, it will return the account number of newly created account.

To consume our web service, we need to create a web service client. CXF provides us with many options to build clients for our services. You can use CXF wsdl2java tool to generate web service client stubs. Make sure you have installed and configured the Apache CXF runtime environment in your system and CXF Tools available in your system path.
wsdl2java -client <wsdl-path>
e.g.
wsdl2java -client http://10.100.3.42:9763/AccountServiceProject-1.0.0/services/account_service?wsdl
You will need to endorse the JAX-WS API 2.2 jar, otherwise run wsdl2java with "-frontend jaxws21" to generate JAX-WS 2.1 compliant code instead.
package org.example.webservice;
/**
* Please modify this class to meet your needs
* This class is not complete
*/
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by Apache CXF 2.5.2
* 2012-06-12T17:33:20.094+05:30
* Generated source version: 2.5.2
*
*/
public final class AccountService_AccountServicePort_Client {
private static final QName SERVICE_NAME = new QName("http://webservice.example.org/", "AccountServiceService");
private AccountService_AccountServicePort_Client() {
}
public static void main(String args[]) throws java.lang.Exception {
URL wsdlURL = AccountServiceService.WSDL_LOCATION;
if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
File wsdlFile = new File(args[0]);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
AccountServiceService ss = new AccountServiceService(wsdlURL, SERVICE_NAME);
AccountService port = ss.getAccountServicePort();
{
System.out.println("Invoking debitAccount...");
int _debitAccount_arg0 = 0;
int _debitAccount_arg1 = 0;
boolean _debitAccount__return = port.debitAccount(_debitAccount_arg0, _debitAccount_arg1);
System.out.println("debitAccount.result=" + _debitAccount__return);
}
{
System.out.println("Invoking checkAccount...");
int _checkAccount_arg0 = 0;
boolean _checkAccount__return = port.checkAccount(_checkAccount_arg0);
System.out.println("checkAccount.result=" + _checkAccount__return);
}
{
System.out.println("Invoking creditAccount...");
int _creditAccount_arg0 = 0;
int _creditAccount_arg1 = 0;
boolean _creditAccount__return = port.creditAccount(_creditAccount_arg0, _creditAccount_arg1);
System.out.println("creditAccount.result=" + _creditAccount__return);
}
{
System.out.println("Invoking createAccount...");
boolean _createAccount_arg0 = false;
int _createAccount__return = port.createAccount(_createAccount_arg0);
System.out.println("createAccount.result=" + _createAccount__return);
}
{
System.out.println("Invoking checkAccountBalance...");
int _checkAccountBalance_arg0 = 0;
int _checkAccountBalance__return = port.checkAccountBalance(_checkAccountBalance_arg0);
System.out.println("checkAccountBalance.result=" + _checkAccountBalance__return);
}
System.exit(0);
}
}
package org.example.webservice;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
public final class AccountService_AccountServicePort_Client {
private static final QName SERVICE_NAME = new QName(
"http://webservice.example.org/", "AccountServiceService");
private AccountService_AccountServicePort_Client() {
}
public static void main(String args[]) throws java.lang.Exception {
URL wsdlURL = AccountServiceService.WSDL_LOCATION;
if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
File wsdlFile = new File(args[0]);
try {
if (wsdlFile.exists()) {
wsdlURL = wsdlFile.toURI().toURL();
} else {
wsdlURL = new URL(args[0]);
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
AccountServiceService ss = new AccountServiceService(wsdlURL,
SERVICE_NAME);
AccountService port = ss.getAccountServicePort();
int accountNo = 0;
int accountBalance = 0;
// Invoke the create account service.
boolean printOnConsole = false; // print details on server's console
accountNo = port.createAccount(printOnConsole);
System.out.println(String.format(
"Account Created Under Account Number :%d", accountNo));
// Invoke the check account service
System.out.println("Verifying Account Existence......");
System.out.println("Account Existence Verifyied : "
+ port.checkAccount(accountNo));
// Invoke the credit account service.
boolean creditAccountResponse = port.creditAccount(accountNo, 20000);
System.out.println("Account Credited by US$ 20000 : "
+ creditAccountResponse);
// Invoke the check account balance service.
accountBalance = port.checkAccountBalance(accountNo);
System.out.println(String.format("Current Account Balance (US$): %d",
accountBalance));
// Invoke the debit account service.
boolean debitAccountResponse = port.debitAccount(accountNo, 8000);
System.out.println("Account Debited by US$ 8000 : "
+ debitAccountResponse);
// Invoke the check account balance service.
accountBalance = port.checkAccountBalance(accountNo);
System.out.println(String.format("Current Account Balance (US$): %d",
accountBalance));
System.exit(0);
}
}
Output of client program would look like this

Web server project
Client sample
In this articale we demonstrate how we use WSO2 Developer Studio to develop, test and deploy Apache CXF JAX Web Services for WSO2 Application Server and consuming the JAX web service using java client program. All the important points were discussed where you can try out the above scenario using given source resources.
Author
Melan Nimesh Jayasingha, Software Engineer