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="http://www.globalcompany.com/customerregistration"
             xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:tns="http://www.globalcompany.com/customerregistration"
             xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:types="http://www.globalcompany.com/customer.xsd">
  <types>
    <schema targetNamespace="http://www.globalcompany.com/customer.xsd"
            xmlns="http://www.w3.org/2001/XMLSchema"
            xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
      <complexType name="CustomerType">
        <sequence>
          <element name="FirstName" type="string"/>
          <element name="LastName" type="string"/>
        </sequence>
      </complexType>
      <element name="CustomerE" type="types:CustomerType"/>
      <element name="validE" type="boolean"/>
      <element name="errorE" type="string"/>
    </schema>
  </types>
  <message name="CustomerRequestMessage">
    <part name="Customer" element="types:CustomerE"/>
  </message>
  <message name="CustomerResponseMessage">
    <part name="valid" element="types:validE"/>
  </message>
  <message name="CustomerFaultMessage">
    <part name="error" element="types:errorE"/>
  </message>
  <portType name="CustomerRegistration">
    <operation name="RegisterCustomer">
      <input message="tns:CustomerRequestMessage"
             name="CustomerInfo"/>
      <output message="tns:CustomerResponseMessage"/>
      <fault message="tns:CustomerFaultMessage"
             name="NotRegistered"/>
    </operation>
  </portType>
  <binding name="CustomerRegistrationSoapHttp" type="tns:CustomerRegistration">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <operation name="RegisterCustomer">
      <soap:operation soapAction="http://www.globalcompany.com/CustomerRegistration"/>
      <input name="CustomerInfo">
        <soap:body use="literal" parts="Customer"/>
      </input>
      <output>
        <soap:body use="literal" parts="valid"/>
      </output>
      <fault name="NotRegistered">
        <soap:fault use="literal" name="NotRegistered"/>
      </fault>
    </operation>
  </binding>
  <service name="CustomerRegistrationService">
    <port name="CustReg"
          binding="tns:CustomerRegistrationSoapHttp">
      <soap:address location="http://oradev201.local.com:7777/cust/CustReg"/>
    </port>
  </service>
</definitions>
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 CustomerRegistrationServiceSOAPImpl.java 
c. Update the code and return a response conforming to the output parameters of this method
Example:
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:
location="http://dev.ipnweb.com:7777/cust/CustomerRegistrationSOAP"
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):
http://dev.ipnweb.com:7777/cust/CustomerRegistrationSOAP
11. Example of SOAP message:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body xmlns:ns1="http://www.globalcompany.com/ns/customer.xsd">
    <ns1:CustomerE>
      <FirstName></FirstName>
      <LastName></LastName>
    </ns1:CustomerE>
  </soap:Body>
</soap:Envelope>


Ahmed Aboulnaga

No comments: