You are on page 1of 26

Web Services

WebServicesarethebasic
fundamentalbuildingblocksofinvoking
featuresthatcanbeaccessedbyan
applicationprogram.
Theaccessibilitytoprovidethe
featuresacrossdifferentplatformsthat
aredevelopedusingdifferent
languages,andcanbeusedbyany
remoteapplication,overtheinternet
andavoidsecurelyrestrictions.

SOAPwebservices
SOAPisessentiallyanXML-basedprotocolfor
invokingremotemethods.(verborientedvs.
nounorientedRESTWS)
Communicationbetweentheserviceandthe
applicationisthroughastandardformatcalled
XML(eXtensibleMarkupLanguage)whichis
universalandisacceptedonanyplatform
acrossthedistributednetwork.

SOAP (Simple Object Access


Protocol)
SOAPisaprotocolbasedonHTTP
protocolthatbymeansofwhichWeb
Servicesisenabled;itistheformatthat
isacclaimeduniversallyduetoitsnature
ofstandardizationandrulesthatit
implements.
.
http://cwiki.apache.org/GMOxDOC20/simp
le-web-service-with-jax-ws.html

UDDI (Universal Discription,


Discovery and Integration):

UDDIisspecificallyagoverningbody
thatmonitorsthepublicationand
discoveryofthewebservices
implementationwithrespecttomessage
communicationbetweentheapplication
andalsoattheenterpriselevel.
WebserviceisdeployedonWebserver
(ApacheTomcat,IIS)orApplication
server(JavaEE,Glassfish,WebLogic)

Simplecalculationwebservice(add,
subtract)
@webservice

WSDL (Web Service Description


Language):

WSDLisanXML-basedlanguagethat
describestheinterfacetoSOAPservices
Itconsistsofthedescriptionaboutthe
webservice,whichisbasicallyafile
with.wsdlextensionthatyoucanfindin
yourapplicationfolder.

WSDL
basics:writteninXML,describeweb
services,locatewebservices
WSDLstructure
<types>datatypesusedbytheWS
<message>messages(I/Oparameters)
<portType>setofoperations(Interface)
<binding>communicationprotocols
usedbytheWS

WSDLstructure(types)
WSDLstructure(types)
<wsdl:types>
<s:schematargetNamespace="http://ws.apache.org/axis2">
<s:elementname="add">
<s:complexType>
<s:sequence>
<s:elementminOccurs="1"maxOccurs="1"name="a"type="xs:int"/>
<s:elementminOccurs="1"maxOccurs="1"name="b"type="xs:int"/>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
...
</wsdl:types>

WSDLstructure(message)
<wsdl:messagename="addRequest">
<wsdl:partname="parameters"
element="ns:add"/>
</wsdl:message>
<wsdl:messagename="addResponse">
<wsdl:partname="parameters"
element="ns:addResponse"/>
</wsdl:message>

WSDLstructure(portType)
<wsdl:portTypename="CalculatorPortType">
<wsdl:operationname="add">
<wsdl:inputmessage="ns:addRequest"wsaw:Action="urn:add"/>
<wsdl:outputmessage="ns:addResponse"
wsaw:Action="urn:addResponse"/>
</wsdl:operation>
<wsdl:operationname="subtract">
<wsdl:inputmessage="ns:subtractRequest"
wsaw:Action="urn:subtract"/>
<wsdl:outputmessage="ns:subtractResponse"
wsaw:Action="urn:subtractResponse"/>
</wsdl:operation>
</wsdl:portType>

Simple Object Access Protocol: structure


SOAP
<?xmlversion="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soapencoding">
<soap:Header>
...
</soap:Header>
<soap:Body>
...
</soap:Body>
</soap:Envelope>

SOAPstructure(request)
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soapenvelope"
xmlns:axis="http://ws.apache.org/axis2">
<soap:Header/>
<soap:Body>
<axis:add>
<axis:param0>45</axis:param0>
<axis:param1>12</axis:param1>
</axis:add>
</soap:Body>
</soap:Envelope>

SOAPstructure(response)
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soapenvelope"
xmlns:axis="http://ws.apache.org/axis2">
<soap:Header/>
<soap:Body>
<axis:addResponse>
<axis:return>57</axis:return>
</axis:addResponse>
</soap:Body>
</soap:Envelope>

AcompleteexampleofJAX-WS
Webmethodintadd(inta,intb),theSOAPrequestandresponsefor
thismethodmightlooklikethefollowing:
SOAPrequest
<?xmlversion="1.0"encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<m:addxmlns:m="http://example/">
<a>1</a>
<b>2</b>
</m:add>
</soapenv:Body>
</soapenv:Envelope>

SOAPresponse
<?xmlversion="1.0"encoding="UTF-8"?>
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/so
ap/envelope/">
<soapenv:Body>
<m:addResponsexmlns:m="http://example/">
<result>3</result>
</m:addResponse>
</soapenv:Body>
</soapenv:Envelope>

namesa<service>,specifiesa<port>(URI)atwhichitisavailable
andreferstoabindingfortheport
<binding>specifiesthe
styleofinteraction(e.g.RPC)
transportprotocolused(e.g.HTTP)
<operation>sdefinedalongwithencodingStylefortheir<input>
and<output>
<portType>specifiesa
setofnamed<operation>swhich
refertothe<message>susedbyeach<operation>for<input>
and<output>
a<message>
describesaone-waymessage(requestorresponse)
consistsofanumberof<part>sreferringtoparametersorreturn
values,eachofwhichhasatype(e.g.xsd:string)
<types>describesalldatatypesusedbetweenclientandserver
(XMLschemaisdefaulttypesystem)

AddserviceWSDL
<?xmlversion="1.0"standalone="yes"?>
<definitionstargetNamespace="http://example/"name="AddService"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns="http://schemas.xmlsoap.org/wsdl/"xmlns:m="http://example/">
<types>
<xsd:schemaversion="1.0"targetNamespace="http://example/">
<xsd:elementname="add"type="m:add"/>
<xsd:complexTypename="add">
<xsd:sequence>
<xsd:elementname="a"type="xsd:int"/>
<xsd:elementname="b"type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
<xsd:elementname="addResponse"type="m:addResponse"/>
<xsd:complexTypename="addResponse"/>
<xsd:sequence>
<xsd:elementname="result"type="xsd:int"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</types>

<bindingname="AddPortBinding"type="m:Add">
<soap:binding
transport="http://schemas.xmlsoap.org/wsdl/soap/"
style="document"/>
<operationname="add">
<soap:operationsoapAction=""/>
<input><soap:bodyuse="literal"/></input>
<output><soap:bodyuse="literal"/></output>
</operation>
</binding>

Servicedefinition
<servicename="AddService">
<portname="AddPort"
binding="m:AddPortBinding">
<soap:address
location="http://example/add-service/"/>
</port>
</service>
</definitions>

Calculatorinterface
TheCalculatorinterfacedefinestheServiceEndpoint
Interface(SEI)fortheWebService.
http://cwiki.apache.org/GMOxDOC20/simple-web-servicewith-jax-ws.html
Calculator.java
packageorg.apache.geronimo.samples.jws;
importjavax.jws.*
@WebService(name="CalculatorPortType",
targetNamespace=
"http://jws.samples.geronimo.apache.org")
publicinterfaceCalculator{
@WebMethod
publicintadd(@WebParam(name="value1")intvalue1,
@WebParam(name="value2")intvalue2);}

TheCalculatorServiceclassimplementstheWeb
Servicebusinesslogic.Itimplementsallthemethods
definedintheSEI.Theclassdoesnotneedto
implementtheCalculatorinterfacebutmustreferenceit
throughthe@WebService.endpointInterfaceannotation.
ThisclasswillbeexposedasaServletthroughweb.xml
fileeventhoughitdoesnotextendthe
javax.servlet.Servletclass.
Thecontextvariablemarkedwiththe@Resource
annotationwillbeinjectedatruntime.The
WebServiceContextcanbeusedtoobtainthemessage
contextandsecurityinformationrelativetothecall.

CalculatorService.java
packageorg.apache.geronimo.samples.jws;
importjavax.annotation.Resource;importjavax.jws.WebService;
importjavax.xml.ws.WebServiceContext;
@WebService(serviceName="Calculator",
portName="CalculatorPort",endpointInterface=
"org.apache.geronimo.samples.jws.Calculator",
targetNamespace="http://jws.samples.geronimo.apache.org",
wsdlLocation="WEB-INF/wsdl/CalculatorService.wsdl")
publicclassCalculatorServiceimplementsCalculator
@ResourceprivateWebServiceContextcontext;
publicintadd(intvalue1,intvalue2)
{System.out.println("UserPrincipal:"+context.getUserPrincipal());

returnvalue1+value2;}}
Theweb.xmldescriptorisusedtodeploytheWebService.Ifthe
web.xmldescriptorisnotprovided,itwillbeautomaticallygenerated
duringdeployment.

JSP-based JAX-WS client


Theadd.jspisabasicclientfortheCalculatorServiceWebService.

<%@pagelanguage="java"contentType="text/html;
charset=ISO-8859-1"pageEncoding="ISO-8859-1"%>
<%@page
import="javax.naming.InitialContext,javax.xml.ws.Service
,org.apache.geronimo.samples.jws.Calculator"%>
<htmlxmlns="http://www.w3.org/1999/xhtml"
xml:lang="en">
<head>
<title>ApacheGeronimoSampleApplication-JAX-WS
Calculator</title>
<metacontent="text/html;CHARSET=iso-8859-1"httpequiv="Content-Type">
</head>

<BODY><fontface="Verdana,Helvetica,Arial">
<formaction="add.jsp">
Value1:<inputtype="text"name="value1">
Value2:<inputtype="text"name="value2">
<inputtype="submit"value="Add">
</form><br>
<%Stringvalue1=request.getParameter("value1");Stringvalue2=
request.getParameter("value2");
if(value1!=null&&value1.trim().length()>0&&value2!=null&&
value2.trim().length()>0){
try{
intv1=Integer.parseInt(value1);
intv2=Integer.parseInt(value2);
InitialContextctx=newInitialContext();
Serviceservice=(Service)ctx.lookup("java:comp/env/services/Calculator");
Calculatorcalc=service.getPort(Calculator.class);
intsum=calc.add(v1,v2);
out.println("Result:"+v1+"+"+v2+"="+sum);}
catch(Exceptione){out.println("Error:"+e.getMessage());});}
%>
</body>
</html>

<messagename="add">
<partname="parameters"element="m:add"/>
</message>
<messagename="addResponse"><part
name="parameters"element="m:addResponse"/>
</message>
<portTypename="Add">
<operationname="add">
<inputmessage="m:add"/>
<outputmessage="m:addResponse"/>
</operation>
</portType>

You might also like