Friday, January 9, 2009

Creating a top-down Java web service

This post describes how to create a top-down webservice in Java. This involves by first creating your WSDL (interface) from which the Java class is then created from.

The instructions describe how to develop this in Oracle JDeveloper 10g and deploy to Oracle Application Server 10g, but they are mostly compatible with Oracle JDeveloper 11g and Oracle WebLogic Server 11g.

1. Open JDeveloper (e.g., double-click on c:\jdev\jdeveloper.exe)

2. Create a new application WebServiceStubs:
a. Right-click on Applications 
b. Click on New Application and then OK 
c. Enter CustomerRegistration for the project name and then OK
3. Create a WSDL document:
a. Right-click on CustomerRegistration 
b. Click on New
c. Choose WSDL Document under Business Tier --> Web Services and then OK 
d. Enter CustomerRegistration and then OK
4. Copy (or create new) your existing WSDL to your working folder:
a. Copy your existing CustomerRegistration.wsdl file (e.g., to c:\jdev\jdev\mywork\WebServiceStubs\CustomerRegistration\src)
Example (CustomerRegistration.wsdl):
<definitions targetNamespace=""
    <schema targetNamespace=""
      <complexType name="CustomerType">
          <element name="FirstName" type="string"/>
          <element name="LastName" type="string"/>
      <element name="CustomerE" type="types:CustomerType"/>
      <element name="validE" type="boolean"/>
      <element name="errorE" type="string"/>
  <message name="CustomerRequestMessage">
    <part name="Customer" element="types:CustomerE"/>
  <message name="CustomerResponseMessage">
    <part name="valid" element="types:validE"/>
  <message name="CustomerFaultMessage">
    <part name="error" element="types:errorE"/>
  <portType name="CustomerRegistration">
    <operation name="RegisterCustomer">
      <input message="tns:CustomerRequestMessage"
      <output message="tns:CustomerResponseMessage"/>
      <fault message="tns:CustomerFaultMessage"
  <binding name="CustomerRegistrationSoapHttp" type="tns:CustomerRegistration">
    <soap:binding style="document" transport=""/>
    <operation name="RegisterCustomer">
      <soap:operation soapAction=""/>
      <input name="CustomerInfo">
        <soap:body use="literal" parts="Customer"/>
        <soap:body use="literal" parts="valid"/>
      <fault name="NotRegistered">
        <soap:fault use="literal" name="NotRegistered"/>
  <service name="CustomerRegistrationService">
    <port name="CustReg"
      <soap:address location=""/>
5. Create a Java Web Service from WSDL:
a. Right-click on CustomerRegistration 
b. Click on New 
c. Choose Java Web Service from WSDL under Business Tier --> Web Services and then OK 
d. Click Next 
e. Click Browse 
f. Double-click on src 
g. Choose CustomerRegistration.wsdl and then Open 
h. Click Next 
i. Check Reuse Existing Type Classes and then Finish
6. Modify code:
a. Click on CustomerRegistrationService 
b. Under the Structure Navigator, double-click on 
c. Update the code and return a response conforming to the output parameters of this method
public boolean registerCustomer(CustomerType customer) throws CustomerFaultMessage {
  if (customer.getFirstName().equals("Hello")) {
    return true;
  } else {
    return false;
7. Check for errors:
a. Right-click on CustomerRegistration 
b. Click on Rebuild
8. Edit deployment files:
a. Double-click on WebServices.deploy under Resources 
b. Choose Specify J2EE Web Context Root and type cust then OK 
c. Double-click on CustomerRegistrationService.wsdl under Web Content --> WEB-INF\wsdl 
d. Click on the Source tab and locate 'soap:address' and update it accordingly. For example:
9. Deploy the service:
a. Click on File --> Save All 
b. Right-click on WebServices.deploy and 'Deploy to' your application server 
c. Click on OK whenever prompted
10. Navigate to the web service and test (using a tool such as SoapUI):
11. Example of SOAP message:
<soap:Envelope xmlns:soap="">
  <soap:Body xmlns:ns1="">

Ahmed Aboulnaga

No comments: