What is SOAP?
- SOAP stands for Simple Object Access Protocol
- SOAP is a communication protocol
- SOAP is for communication between applications
- SOAP is a format for sending messages
- SOAP communicates via Internet
- SOAP is platform independent
- SOAP is language independent
- SOAP is based on XML
- SOAP is simple and extensible
- SOAP allows you to get around firewalls
- SOAP is a W3C recommendation
Review WSDL
Services are defined
using six major elements:
- types, which provides data type definitions used to describe
the messages exchanged.
- message,
which represents an abstract definition of the data being transmitted. A
message consists of logical parts, each of which is associated with a
definition within some type system.
- portType,
which is a set of abstract operations. Each operation refers to an input
message and output messages.
- binding,
which specifies concrete protocol and data format specifications for the
operations and messages defined by a particular portType.
- port,
which specifies an address for a binding, thus defining a single communication
endpoint.
- service,
which is used to aggregate a set of related ports.
<?xml version="1.0"
encoding="utf-8"?>
<wsdl:definitions
xmlns:s="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
xmlns:tns="https://taxes.test.com/test/webservices/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
targetNamespace="https://taxes.test.com/test/webservices/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema
elementFormDefault="qualified"
targetNamespace="https://taxes.test.com/test/webservices/">
<s:element
name="IdHdr" type="tns:IdHdr" />
<s:complexType
name="IdHdr">
<s:sequence>
<s:element
minOccurs="0" maxOccurs="1" name="token"
type="s:string" />
<s:element
minOccurs="0" maxOccurs="1" name="address"
type="s:string" />
</s:sequence>
</s:complexType>
<s:element name="GetSQList">
element names
became classes
<s:complexType />
</s:element>
<s:element
name="GetSQListResponse">
<s:complexType>
<s:sequence>
<s:element
minOccurs="0" maxOccurs="1"
name="GetSQListResult" type="s:string" />
<s:element
minOccurs="0" maxOccurs="1" name="processMessage"
type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
<wsdl:message
name="GetSQListSoapIn">
<wsdl:part
name="parameters" element="tns:GetSQList" />
</wsdl:message>
<wsdl:message
name="GetSQListSoapOut">
<wsdl:part
name="parameters" element="tns:GetSQListResponse" />
</wsdl:message>
<wsdl:message
name="GetSQListIdHdr">
<wsdl:part
name="IdHdr" element="tns:IdHdr" />
</wsdl:message>
<wsdl:portType name="OTPWebServiceSoap">
This became the interface
<wsdl:operation
name="GetSQList"> This became method
in the above interface
<wsdl:documentation
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">-- Get List of
Security Questions --</wsdl:documentation>
<wsdl:input
message="tns:GetSQListSoapIn" />
<wsdl:output
message="tns:GetSQListSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding
name="OTPWebServiceSoap" type="tns:OTPWebServiceSoap">
<soap:binding
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation
name="GetSQList">
<soap:operation
soapAction="https://taxes.test.com/test/webservices/GetSQList"
style="document" />
<wsdl:input>
<soap:body
use="literal" />
<soap:header
message="tns:GetSQListIdHdr" part="IdHdr"
use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body
use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="OTPWebService"> This will be the class with WSDL location and all
<wsdl:port
name="OTPWebServiceSoap"
binding="tns:OTPWebServiceSoap">
<soap:address
location="http://localhost/test/webservices/otpwebservice.asmx"
/>
</wsdl:port> soap:address location Will became the Endpoint
</wsdl:service>
</wsdl:definitions>
- Type
- Message
- Porttype
- Binding
- Service
Type : The data types used by the web service. We have all xsd imports here
Message : The messages used by the web service
<wsdl:message name="HelloWorldRequest">
<wsdl:part name="header" element="am:TransactionHeader"></wsdl:part>
<wsdl:part name="parameters" element="amt:HelloWorldRequest"></wsdl:part>
</wsdl:message>
<wsdl:message name="HelloWorldResponse">
<wsdl:part name="parameters" element="amt:HelloWorldResponse"></wsdl:part>
</wsdl:message>
PortTpe : The operations performed by the web service
<wsdl:portType name="HelloPort">
<wsdl:operation name="HelloWorld">
<wsdl:input message="am:HelloWorldRequest" />
<wsdl:output message="am:HelloWorldResponse" />
</wsdl:operation>
</wsdl:portType>
Binding: The communication protocols used by the web service and message format.
Service : A collection of related end points
Documentation
WSDL uses the optional wsdl:document element
as a container for human readable documentation. The content of the element is
arbitrary text and elements ("mixed" in XSD). The documentation
element is allowed inside any WSDL language element.
SOAPAction
<wsdl:binding name="OTPWebServiceSoap" type="tns:OTPWebServiceSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetVersion">
<soap:operation soapAction="https://taxes.test.com/test/webservices/GetVersion" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
<soap:header message="tns:GetVersionIdHdr" part="IdHdr" use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
The SOAPAction URI is used to uniquely identify
the particular operation of a service. As stated in the specification,. it is
must for SOAP over HTTP.
SOAPAction is generally a combination of a web-service namespace and operation name like - http://abc.com/ws/ServiceName/1.0/OperationName wherehttp://abc.com/ws/ServiceName/1.0 is a meaningful namespace which represents that service "ServiceName" is a web-service (ws) of enterprise abc and it's version is 1.0
Above one is an example only and it may vary across the organizations and implementations because of the standards but ultimately it is represented in that context only whereas location attribute of soap:address element is always an address where service is available. In case of SOAP over HTTP, location attribute will hold a HTTP/HTTPS URL where this service is exposed and for any user to consume this service, will have to hit that URL.
SOAPAction is generally a combination of a web-service namespace and operation name like - http://abc.com/ws/ServiceName/1.0/OperationName wherehttp://abc.com/ws/ServiceName/1.0 is a meaningful namespace which represents that service "ServiceName" is a web-service (ws) of enterprise abc and it's version is 1.0
Above one is an example only and it may vary across the organizations and implementations because of the standards but ultimately it is represented in that context only whereas location attribute of soap:address element is always an address where service is available. In case of SOAP over HTTP, location attribute will hold a HTTP/HTTPS URL where this service is exposed and for any user to consume this service, will have to hit that URL.
Q: Difference between RPC and document style in WSDL Binding
The <wsdl:binding> element of the WSDL contains a pair of parameters that influence the form of the resulting SOAP messages: binding style (RPC or document) and use (encoded or literal).
Style. Mostly we will be using document not RPC
Document: the content of <soap:Body> is specified by XML Schema defined in the <wsdl:type> section. It does not need to follow specific SOAP conventions.
RPC: The structure of an RPC style <soap:Body> element needs to obay some specifications
Use
use="literal" means that the type definitions literally follow an XML schema definition.
use="encoded" refers to the representation of application data in XML, usually according to the SOAP encoding rules.
How to resolve NameConflicts while generating stubs?
Error
Thrown by JAXB : A class/interface with the same name "com.." is already in use. Use a class customization to resolve this conflict. at line 945 column 7 of schema http://.../amtpf.asmx?WSDL
Use the below extra arg in pom.xml to solve this
<extraargs>
<extraarg>-autoNameResolution</extraarg>
</extraargs>
Or Use a binding xml like below so that you will have control over the class names
<bindingFiles>
<bindingFile>
${basedir}/resources/sample jaxws_binding.xml
</bindingFile>
</bindingFiles>
Sample sample jaxws_binding.xml
<jaxws:bindings wsdlLocation="w2.wsdl"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb
http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd
http://java.sun.com/xml/ns/jaxws
http://java.sun.com/xml/ns/jaxws/wsdl_customizationschema_2_0.xsd">
<jaxws:bindings
node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://schemas.test.com/DDM/Tax']">
<jaxb:bindings
node="xs:complexType[@name='GL1099R']/xs:sequence/xs:element[@name='RECID']">
<jaxb:property name="DRTType"/>
</jaxb:bindings>
<jaxb:bindings
node="xs:complexType[@name='FDB_L5_DIVIDEND']/xs:sequence/xs:element[@name='RECID']">
<jaxb:property name="RECID22"/>
</jaxb:bindings>
How to Add soap header?
Add this in pom.xml
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/resources/w2.wsdl</wsdl>
<extendedSoapHeaders>true</extendedSoapHeaders>
</wsdlOption>
</wsdlOptions>
How to give custom wsdl URL path
<wsdlOption>
<wsdl>${basedir}/resources/amtpf.wsdl</wsdl>
</wsdlOption>
And in the Client side Use the below code
URL url = OTPWebService.class.getClassLoader().getResource("createAccount.wsdl");
if (url == null) {
System.out.println("Can not initialize the default wsdl "+OTPWebService.class.getClassLoader().getResource("createAccount.wsdl"));
}
WSDL_LOCATION = url;
How to make WSDL location null
Map<String, Object> requestContext = ((BindingProvider)otpServiceSoap).getRequestContext();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://domain/webservices/otpwebservice.asmx");
How to resolve NameConflicts while generating stubs?
Error
Thrown by JAXB : A class/interface with the same name "com.." is already in use. Use a class customization to resolve this conflict. at line 945 column 7 of schema http://.../amtpf.asmx?WSDL
Use the below extra arg in pom.xml to solve this
<extraargs>
<extraarg>-autoNameResolution</extraarg>
</extraargs>
Or Use a binding xml like below so that you will have control over the class names
<bindingFiles>
<bindingFile>
${basedir}/resources/sample jaxws_binding.xml
</bindingFile>
</bindingFiles>
Sample sample jaxws_binding.xml
<jaxws:bindings wsdlLocation="w2.wsdl"
xmlns:jaxws="http://java.sun.com/xml/ns/jaxws"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb
http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd
http://java.sun.com/xml/ns/jaxws
http://java.sun.com/xml/ns/jaxws/wsdl_customizationschema_2_0.xsd">
<jaxws:bindings
node="wsdl:definitions/wsdl:types/xs:schema[@targetNamespace='http://schemas.test.com/DDM/Tax']">
<jaxb:bindings
node="xs:complexType[@name='GL1099R']/xs:sequence/xs:element[@name='RECID']">
<jaxb:property name="DRTType"/>
</jaxb:bindings>
<jaxb:bindings
node="xs:complexType[@name='FDB_L5_DIVIDEND']/xs:sequence/xs:element[@name='RECID']">
<jaxb:property name="RECID22"/>
</jaxb:bindings>
How to Add soap header?
Add this in pom.xml
<wsdlOptions>
<wsdlOption>
<wsdl>${basedir}/resources/w2.wsdl</wsdl>
<extendedSoapHeaders>true</extendedSoapHeaders>
</wsdlOption>
</wsdlOptions>
How to give custom wsdl URL path
<wsdlOption>
<wsdl>${basedir}/resources/amtpf.wsdl</wsdl>
<wsdlLocation>classpath/createAccount.wsdl</wsdlLocation>
</wsdlOption>
And in the Client side Use the below code
URL url = OTPWebService.class.getClassLoader().getResource("createAccount.wsdl");
if (url == null) {
System.out.println("Can not initialize the default wsdl "+OTPWebService.class.getClassLoader().getResource("createAccount.wsdl"));
}
WSDL_LOCATION = url;
How to make WSDL location null
<extraargs>
<extraarg>-client</extraarg>
<extraarg>-wsdlLocation</extraarg>
<wsdlurl />
</extraargs>
How to set dynamic endpoint in WS Client?Map<String, Object> requestContext = ((BindingProvider)otpServiceSoap).getRequestContext();
requestContext.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://domain/webservices/otpwebservice.asmx");
Thanks Admin this was really a helpful post where i learned a lot from this one and my friend say's that Java Web Services Online Trainingby Hyderabadsys helps us to gain the knowledge as 2 years experienced with their +15 faculty and Live projects. This may Help others too.
ReplyDeleteContact :
India : +91 9030400777
US : +1-347-606-2716
Email: contact@Hyderabadsys.com