Wednesday, July 08, 2020

How to expose WSDL as webservice using ApacheAxis2

Lets assume we have a WSDL file called as AreaService.wsdl
1:- AreaService.wsdl
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://tempuri.org/AreaService/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="AreaService" targetNamespace="http://tempuri.org/AreaService/">
<wsdl:types>
<xsd:schema targetNamespace="http://tempuri.org/AreaService/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="area" type="xsd:float"/>
<xsd:element name="parameters" type="tns:dimensions"/>
<xsd:complexType name="dimensions">
<xsd:sequence>
<xsd:element name="width" type="xsd:float"></xsd:element>
<xsd:element name="height" type="xsd:float"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="CalculateRectAreaResponse">
<wsdl:part element="tns:area" name="area"/>
</wsdl:message>
<wsdl:message name="CalculateRectAreaRequest">
<wsdl:part element="tns:parameters" name="parameters"/>
</wsdl:message>
<wsdl:portType name="AreaService">
<wsdl:operation name="CalculateRectArea">
<wsdl:input message="tns:CalculateRectAreaRequest"/>
<wsdl:output message="tns:CalculateRectAreaResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="AreaServiceSOAP" type="tns:AreaService">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="CalculateRectArea">
<soap:operation soapAction="http://tempuri.org/AreaService/NewOperation"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="AreaService">
<wsdl:port binding="tns:AreaServiceSOAP" name="AreaServiceSOAP">
<soap:address location="http://tempuri.org"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

Follow below given step to expose this wsdl file as SOAP W/S using Apache Axis2.
Make sure you have WTP and Axis plug in Eclipse and also have Axis2 set up in your machine.
1- Create Dynamic Project
Image1Image2
Click on Modify and select Axis2
Image3
Click save and next
Image4Image5
Click on Finish Button.
Now create new folder wsdl as shown below
Image6
and click finish button.
Copy your wsdl in this wsdl folder
Now lets exposed this wsdl as webservice using following steps given below
Right Click on WSDL file --> Other --> Web Service--> Web Service

Image7Image8Image9.1
Click on web service runtime:Apache Axis2

Image9Image10Image11
Click on start server button and wait for server to start

Image12

Now modify the response of the code generated from the wsdl so that we can get the desired output in this case we have file called AreaServiceSkeleton.java
Area objArea = new Area();
objArea.setArea(parameters.getParameters().getHeight()*parameters.getParameters().getWidth());
return objArea;
//throw new java.lang.UnsupportedOperationException("Please implement " + this.getClass().getName() + "#calculateRectArea");
Image13

Now deploy the applicaiton on the server i.e. right click the project and select run as and tomcat and next and deploy
Now lets check our WSDL files exposed using this url
Image14.1Image14
Click on service
Image15
Now if you hover your mouser cursor on AreaService you will get the WSDL url copy it
http://localhost:8181/SiddhuExposedWsdl/services/AreaService?wsdl
We will use this to check if the service is working or not. To test the SOAP WS we are using SOAP UI tool
Image18Image19Image20

No comments: