May 202016

ADM

How to create WSDL file with Eclipse

  • 20 May 2016
  • ADM

 

 

 

How to create WSDL file with Eclipse - images/logos/eclipse.jpg

 

WSDL is an XML format for describing network services as a set of endpoints operating on messages containing either document-oriented or procedure-oriented information. WSDL stands for Web Services Description Language. The WSDL is an abstract definitions of ports and messages, being separated from the concrete implementation. Using this model to define the service allows the reuse of these definitions and also will allow to implement server and client independently.

Problem

For this tutorial will define a simple payment system with three methods:

  • BalanceInquiry - to get the amount that need to be paid;
  • Payment - the payment;
  • Reversal - to cancel a payment;

The flows for this system would be:

  • make a BalanceInquiry call to see how much is to pay;
  • make a Payment call with the amount you want to pay;
  • Make a Reversal call if you want to cancel the payment;

For each method will define a few simple fields.

BalanceInquiry - Request

  • tranNr - unique field to identify the transaction;
  • account - the account to interrogate the balance for;

BalanceInquiry - Response

  • tranNr - unique field to identify the transaction;
  • account - the account the balance is for;
  • balance - the amount balance;
  • status:
    • code - the response code;
    • message - the message;

Payment - Request

  • tranNr -unique field to identify the transaction;
  • amount - the amount to pay;
  • account - the account to be paid from;

Payment - Response

  • tranNr - unique field to identify the transaction;
  • amount - the amount paid;
  • account - the account to be paid from;
  • status:
    • code - the response code;
    • message - the message;

Reversal - Request

  • tranNr - unique field to identify the transaction;
  • origTranNr - original transaction number;
  • account - the account to be reversed from;

Reversal - Response

  • tranNr - unique field to identify the transaction;
  • account - the account to be reversed from;
  • status:
    • code - the response code;
    • message - the message;

Solution

For this I will use Eclipse, but make sure you have Eclipse Java EE for Web Developers edition.

Step1 - New WSDL File

First thing we need to create a project to contain the WSDL document. It does not matter what kind of project we create. Then in the workbench, click File -> New -> Other and select Web Services -> WSDL. Click Next.

How to create WSDL file with Eclipse - /images/newWSDL1.png

Select the project or folder that will contain the WSDL file. In the File name field, type the name of the WSDL file. Will choose InvoiceService.wsdl for this tutorial. The name of the XML file must end in .wsdl

How to create WSDL file with Eclipse - /images/newWSDL2.png

On the next step will choose Target namespace, Prefix, Protocol and SOAP Binding Options. Don't worry about naming, everithing can be changed after.

How to create WSDL file with Eclipse - /images/newWSDL3.png

The auto generated file will look like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:in="http://www.example.org/InvoiceService/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="InvoiceService" targetNamespace="http://www.example.org/InvoiceService/">
  <wsdl:message name="NewOperationRequest">
    <wsdl:part name="NewOperationRequest" type="xsd:string"/>
  </wsdl:message>
  <wsdl:message name="NewOperationResponse">
    <wsdl:part name="NewOperationResponse" type="xsd:string"/>
  </wsdl:message>
  <wsdl:portType name="InvoiceService">
    <wsdl:operation name="NewOperation">
      <wsdl:input message="in:NewOperationRequest"/>
      <wsdl:output message="in:NewOperationResponse"/>
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="InvoiceServiceSOAP" type="in:InvoiceService">
    <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="NewOperation">
      <soap:operation soapAction="http://www.example.org/InvoiceService/NewOperation"/>
      <wsdl:input>
        <soap:body namespace="http://www.example.org/InvoiceService/" use="literal"/>
      </wsdl:input>
      <wsdl:output>
        <soap:body namespace="http://www.example.org/InvoiceService/" use="literal"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
  <wsdl:service name="InvoiceService">
    <wsdl:port binding="in:InvoiceServiceSOAP" name="InvoiceServiceSOAP">
      <soap:address location="http://www.example.org/"/>
    </wsdl:port>
  </wsdl:service>
</wsdl:definitions>

and the design view like this:

How to create WSDL file with Eclipse - /images/newWSDL4.png

Step2 - Define all the fields

In this step will define all fields used in the WSDL.

Will do this by adding a new schema from Outline View.

How to create WSDL file with Eclipse - /images/newWSDL5.png

After you add the new schema, right click on it and open.

How to create WSDL file with Eclipse - /images/newWSDL6.png

Define an simple type

Here will define all simple types that will appear in the WSDL. e.g. amount, balance.

  • right click on Type area -> Add Simple Type;
  • from Properties View set up all the parameters needed.

The element for our first element defined 'code' will be:

<xsd:simpleType name="code">
   <xsd:restriction base="xsd:string">
      <xsd:length value="2"></xsd:length>
   </xsd:restriction>
</xsd:simpleType>

Using these steps will add all the simple types needed needed: code, message, account are strings. amount and balance are double, tranNr and OrigTranNr are int.

Define an complex type

Here will define all complex types that will appear in the WSDL. In our case the status field.

  • right click on Types area -> Add Complex Type;
  • set up the name;
  • from Outline View right click on the type added -> Add Element;
  • from Properties View select the Type needed. (our case add two elements: code and message);

The status element will look like this.

<xsd:complexType name="status">
   <xsd:sequence>
      <xsd:element name="code" type="inv:code"></xsd:element>
      <xsd:element name="message" type="inv:message"></xsd:element>
   </xsd:sequence>
</xsd:complexType>

How to create WSDL file with Eclipse - /images/newWSDL7.png

Step3 - Define all the methods

After we defined our elements we can start to define all methods needed.

  • From Outline View -> Port Type -> right click on the service -> Add Operation
  • From Outline View -> Bidings-> right click on the biding -> Generate Biding Content ...

Conclusions

Now, as we have the WSDL file created it remains to implement the server, client and to create a mock service using SoapUI with dynamic responses for test purpose.

The full print for the WSDL file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:inv="/services/InvoiceService/"
   xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
   xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="InvoiceService"
   targetNamespace="/services/InvoiceService/">
   <wsdl:types>
      <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         targetNamespace="/services/InvoiceService/">

         <xsd:simpleType name="code">
            <xsd:restriction base="xsd:string">
               <xsd:length value="2"></xsd:length>
            </xsd:restriction>
         </xsd:simpleType>

         <xsd:simpleType name="message">
            <xsd:restriction base="xsd:string">
               <xsd:minLength value="1"></xsd:minLength>
            </xsd:restriction>
         </xsd:simpleType>

         <xsd:simpleType name="tranNr">
            <xsd:restriction base="xsd:int">
               <xsd:minExclusive value="1"></xsd:minExclusive>
               <xsd:maxExclusive value="1000000"></xsd:maxExclusive>
            </xsd:restriction>
         </xsd:simpleType>

         <xsd:simpleType name="origTranNr">
            <xsd:restriction base="xsd:int">
               <xsd:minInclusive value="1"></xsd:minInclusive>
               <xsd:maxInclusive value="1000000"></xsd:maxInclusive>
            </xsd:restriction>
         </xsd:simpleType>

         <xsd:simpleType name="account">
            <xsd:restriction base="xsd:string">
               <xsd:minLength value="5"></xsd:minLength>
               <xsd:maxLength value="10"></xsd:maxLength>
            </xsd:restriction>
         </xsd:simpleType>

         <xsd:simpleType name="balance">
            <xsd:restriction base="xsd:double">
               <xsd:minInclusive value="0"></xsd:minInclusive>
               <xsd:maxInclusive value="1000000"></xsd:maxInclusive>
            </xsd:restriction>
         </xsd:simpleType>

         <xsd:simpleType name="amount">
            <xsd:restriction base="xsd:double">
               <xsd:minInclusive value="0"></xsd:minInclusive>
               <xsd:maxInclusive value="1000000"></xsd:maxInclusive>
            </xsd:restriction>
         </xsd:simpleType>

         <xsd:complexType name="status">
            <xsd:sequence>
               <xsd:element name="code" type="inv:code"></xsd:element>
               <xsd:element name="message" type="inv:message"></xsd:element>
            </xsd:sequence>
         </xsd:complexType>

         <xsd:complexType name="BalanceInqueryRequest">
            <xsd:sequence>
               <xsd:element name="account" type="inv:account"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="tranNr" type="inv:tranNr"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>

            </xsd:sequence>
         </xsd:complexType>
         <xsd:complexType name="BalanceInqueryResponse">
            <xsd:sequence>
               <xsd:element name="account" type="inv:account"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="tranNr" type="inv:tranNr"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="balance" type="inv:balance"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="status" type="inv:status"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
            </xsd:sequence>
         </xsd:complexType>
         <xsd:complexType name="PaymentRequest">
            <xsd:sequence>
               <xsd:element name="account" type="inv:account"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="amount" type="inv:amount"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="tranNr" type="inv:tranNr"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>

            </xsd:sequence>
         </xsd:complexType>
         <xsd:complexType name="PaymentResponse">
            <xsd:sequence>
               <xsd:element name="account" type="inv:account"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="tranNr" type="inv:tranNr"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="amount" type="inv:amount"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="status" type="inv:status"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
            </xsd:sequence>
         </xsd:complexType>
         <xsd:complexType name="ReversalRequest">
            <xsd:sequence>
               <xsd:element name="account" type="inv:account"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="origTranNr" type="inv:origTranNr"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="tranNr" type="inv:tranNr"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>

            </xsd:sequence>
         </xsd:complexType>
         <xsd:complexType name="ReversalResponse">
            <xsd:sequence>
               <xsd:element name="account" type="inv:account"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="tranNr" type="inv:tranNr"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
               <xsd:element name="status" type="inv:status"
                  minOccurs="1" maxOccurs="1">
               </xsd:element>
            </xsd:sequence>
         </xsd:complexType>
      </xsd:schema>
   </wsdl:types>
   <wsdl:message name="BalanceInqueryRequest">
      <wsdl:part name="Request" type="inv:BalanceInqueryRequest" />
   </wsdl:message>
   <wsdl:message name="BalanceInqueryResponse">
      <wsdl:part name="Response" type="inv:BalanceInqueryResponse" />
   </wsdl:message>
   <wsdl:message name="PaymentRequest">
      <wsdl:part name="Request" type="inv:PaymentRequest" />
   </wsdl:message>
   <wsdl:message name="PaymentResponse">
      <wsdl:part name="Response" type="inv:PaymentResponse" />
   </wsdl:message>
   <wsdl:message name="ReversalRequest">
      <wsdl:part name="Request" type="inv:ReversalRequest" />
   </wsdl:message>
   <wsdl:message name="ReversalResponse">
      <wsdl:part name="Response" type="inv:ReversalResponse" />
   </wsdl:message>
   <wsdl:portType name="InvoiceService">
      <wsdl:operation name="BalanceInquiry">
         <wsdl:input message="inv:BalanceInqueryRequest" />
         <wsdl:output message="inv:BalanceInqueryResponse" />
      </wsdl:operation>
      <wsdl:operation name="Payment">
         <wsdl:input message="inv:PaymentRequest" />
         <wsdl:output message="inv:PaymentResponse" />
      </wsdl:operation>
      <wsdl:operation name="Reversal">
         <wsdl:input message="inv:ReversalRequest" />
         <wsdl:output message="inv:ReversalResponse" />
      </wsdl:operation>
   </wsdl:portType>
   <wsdl:binding name="InvoiceServiceSOAP" type="inv:InvoiceService">
      <soap:binding style="rpc"
         transport="http://schemas.xmlsoap.org/soap/http" />
      <wsdl:operation name="BalanceInquiry">
         <soap:operation
            soapAction="/services/InvoiceService/BalanceInquiry" />
         <wsdl:input>
            <soap:body use="literal"
               namespace="/services/InvoiceService/" />
         </wsdl:input>
         <wsdl:output>
            <soap:body use="literal"
               namespace="/services/InvoiceService/" />
         </wsdl:output>
      </wsdl:operation>
      <wsdl:operation name="Payment">
         <soap:operation
            soapAction="/services/InvoiceService/Payment" />
         <wsdl:input>
            <soap:body use="literal"
               namespace="/services/InvoiceService/" />
         </wsdl:input>
         <wsdl:output>
            <soap:body use="literal"
               namespace="/services/InvoiceService/" />
         </wsdl:output>
      </wsdl:operation>
      <wsdl:operation name="Reversal">
         <soap:operation
            soapAction="/services/InvoiceService/Reversal" />
         <wsdl:input>
            <soap:body use="literal"
               namespace="/services/InvoiceService/" />
         </wsdl:input>
         <wsdl:output>
            <soap:body use="literal"
               namespace="/services/InvoiceService/" />
         </wsdl:output>
      </wsdl:operation>
   </wsdl:binding>
   <wsdl:service name="InvoiceService">
      <wsdl:port binding="inv:InvoiceServiceSOAP" name="InvoiceServiceSOAP">
         <soap:address location="http://www.example.org/" />
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>