Web Service : First attempt
This article records my first attempt with web service in course SE3353.
1. SOAP
SOAP is an XML-based application-level protocol for accessing web services. It is normally sent on HTTP, but could be sent over SMTP or even FTP.
1.1. SOAP vs REST
They are not mutual-exclusive.
2. WSDL
WSDL, or Web Service Description Language, is an XML based definition language. It’s used for describing the functionality of a SOAP based web service, some important ones being:
schema
: Describes the parameters and return type of services.portType
: Describes- Name of the service
- The operations included in the service
- The input and output for each operation
binding
: Describes the transport protocol that the operations use.service
: Describesaddress
: Where to send HTTP request to call the webservice.
3. SOAP with JS
Using SOAP with JS is quite troublesome, because the xml has to be hand-written or generated with third-party library, and there’s no tool to process WSDL (that I’ve found), you have to read it word by word.
3.1. Preflight OPTIONS
request
Using jquery.soap.
When sending xml
with POST
request, a preflight request will be sent using OPTIONS
method.
3.2. Request namespace
The targetNamespace should be specified for a method call. In jquery.soap
, just add:
namespaceURL: <your-target-namespace>
In the parameters, namespace are also required (server side). See details.
3.3. CXF pitfalls
- Java class for
complexType
in xml must have setters to be generated properly.
3.4. Handling SOAP response xml with JS
I’m using xml2json
jquery plugin to parse xml to json.
Pitfalls:
- When an xml array contains only one element, the library will parse it as a single object.
- All fields of the obejct are of type
string
.