Sunday, March 13, 2016

SOA: Basics: Creating a WSDL using a XSD

This is one of the basic requirements for SOA developers. Today, we will learn how to generate a WSDL file using a XSD. We will use Eclispe to achieve this. So, let's begin!

Our Objective:
To build a HelloWorld WSDL that will have the following inputs, outputs and operations:
  • Inputs:
    • First Name
    • Last Name
  • Outputs:
    • GreetingsMessage
  • Operations:
    • sayGreetings






















Creating the XSD

Create a new Dynamic Web Project > Provide a Project Name (HelloWorldSvc) > Click Finish
Right click the WEB-INF > Click New > Other > Select XML Schema File

Create HelloWorldRequest.xsd


Add the firstName and lastName elements to the HelloWorldRequest.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/HelloWorldRequest" xmlns:tns="http://www.example.org/HelloWorldRequest" elementFormDefault="qualified">
    <element name="hw_Request" type="tns:hw_Request" />
    <complexType name="hw_Request">
    <sequence>
    <element name="firstName" type="string" minOccurs="1" maxOccurs="1"></element>
    <element name="lastName" type="string" minOccurs="1" maxOccurs="1"></element>
    </sequence>
    </complexType>
</schema>


Similarly,
Create HelloWorldResponse.xsd


Add the following elements to the HelloWorldResponse.xsd

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/HelloWorldResponse" xmlns:tns="http://www.example.org/HelloWorldResponse" elementFormDefault="qualified">
    <element name="hw_Response" type="string" />
</schema>


Creating the WSDL

Right click the WEB-INF > Click New > Other > WSDL
Provide a name for the WSDL and click Finish

Follow these steps to update the WSDL:

Add the namespace for the request and response xsd to the wsdl

<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.example.org/HelloWorld/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorld"
targetNamespace="http://www.example.org/HelloWorld/" xmlns:request="http://www.example.org/HelloWorldRequest"
xmlns:response="http://www.example.org/HelloWorldResponse">

Import the schemas for the request and response

<wsdl:types>
<xsd:schema targetNamespace="http://www.example.org/HelloWorld/">
<xsd:import namespace="http://www.example.org/HelloWorldRequest" schemaLocation="../xsd/HelloWorldRequest.xsd" />
<xsd:import namespace="http://www.example.org/HelloWorldResponse" schemaLocation="../xsd/HelloWorldResponse.xsd" />
</xsd:schema>
</wsdl:types>

Add the message for the request and response messages

<wsdl:message name="sayGreetingsRequest">
<wsdl:part element="request:hw_Request" name="parameters" />
</wsdl:message>
<wsdl:message name="sayGreetingsResponse">
<wsdl:part element="response:hw_Response" name="parameters" />
</wsdl:message>

Update the process name and message binding
<wsdl:portType name="HelloWorld">
<wsdl:operation name="sayGreetings">
<wsdl:input message="tns:sayGreetingsRequest" />
<wsdl:output message="tns:sayGreetingsResponse" />
</wsdl:operation>
</wsdl:portType>


Generated Output/ Final WSDL

And your WSDL is ready! Here's how it looks!

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.example.org/HelloWorld/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="HelloWorld"
targetNamespace="http://www.example.org/HelloWorld/" xmlns:request="http://www.example.org/HelloWorldRequest"
xmlns:response="http://www.example.org/HelloWorldResponse">
<wsdl:types>
<xsd:schema targetNamespace="http://www.example.org/HelloWorld/">
<xsd:import namespace="http://www.example.org/HelloWorldRequest"
schemaLocation="../xsd/HelloWorldRequest.xsd"></xsd:import>
<xsd:import namespace="http://www.example.org/HelloWorldResponse"
schemaLocation="../xsd/HelloWorldResponse.xsd"></xsd:import>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayGreetingsRequest">
<wsdl:part element="request:hw_Request" name="parameters" />
</wsdl:message>
<wsdl:message name="sayGreetingsResponse">
<wsdl:part element="response:hw_Response" name="parameters" />
</wsdl:message>
<wsdl:portType name="HelloWorld">
<wsdl:operation name="sayGreetings">
<wsdl:input message="tns:sayGreetingsRequest" />
<wsdl:output message="tns:sayGreetingsResponse" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloWorldSOAP" type="tns:HelloWorld">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="sayGreetings">
<soap:operation soapAction="http://www.example.org/HelloWorld/sayGreetings" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloWorld">
<wsdl:port binding="tns:HelloWorldSOAP" name="HelloWorldSOAP">
<soap:address location="http://www.example.org/" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>



Hope you find this information useful.Please share your feedback below. I would love to hear from you.

CaptiveCode

No comments:

Post a Comment