收集最实用的网页特效代码!developing interoperable web services - integrating java and microsoft .net
interoperability is one of the main promises of web services. web services are designed to be independent of the underlying operating system and programming language. in this article we will introduce basic web services interoperability issues. we will focus on the two most popular platforms - java and microsoft .net.
introduction
web services interoperability can be divided into two base categories: soap interoperability and wsdl interoperability.
in our previous articles we learned that soap is a very high-level protocol that mandates the structure of xml documents exchanged over some transport protocol. that's quite a vague definition. the vagueness of this definition makes soap extremely extensible and versatile, but it also makes interoperability a bit challenging. at a basic level, we start our quest to achieve interoperability at the transport protocol level. both parties involved in a message exchange must agree to use the same transport protocol, such as http, smtp, or jms. but transport protocol compatibility doesn't necessarily ensure interoperability. there are other issues that affect interoperability. one example is data type encoding. an encoding style defines how programmatic data types are encoded in xml. the soap specification does not mandate any particular encoding style, and developers are free to use any encoding style they want. in theory there are virtually unlimited choices of encoding styles that are mutually incompatible. fortunately there is a standard convention. section 5 of the soap specification defines an encoding style, and it has became the de facto standard for soap encoding. this encoding provides the basis for the automated soap interoperability tests that are periodically carried out by the major soap solutions vendors. you can see these test results online at http://soap.systinet.net/interop/soap/index.html
more and more we're finding that wsdl lies at the heart of web services interoperability. wsdl is the description language for web services. usually a wsdl document is automatically generated by web services framework tools (e.g., wasp wsdlcompiler) from the code written in a particular programming language. developers can use the wsdl document to generate client-side stubs or proxies, which provide convenient access to web services. thus the key to enabling seamless web services interoperability is the ability of one web services framework to consume the wsdl documents generated by other frameworks. the wsdl interoperability effort is just taking off. you can see further details at http://soap.systinet.net/interop/wsdl/index.html.
although there are still some interoperability issues to be resolved, the vendors are progressing very quickly, and we can expect delivery on the promise of web services interoperability very soon.
how to not get trapped
the following subchapters give you some basic tips on how to write interoperable web services using today's web services frameworks. these tips may significantly ease your life as well as the lives of other developers who will use your web services. hopefully some of those tips will be outdated soon.
keep your types simple - avoid advanced xml schema constructs
the xml schema standard is very complex and difficult to implement. moreover, xml schema processing is quite time consuming, so many frameworks sacrifice full xml schema support for performance. some advanced xml schema constructs (e.g., choice) are quite hard to express in a programming language, and few web services frameworks support them. so the key success factor in web services interoperability is to use basic datatypes, such as primitive datatypes, arrays, and structures. as a best practice, decompose the complex types in your interfaces into simple and clean interfaces with basic datatypes. also avoid using specific techniques (e.g. inout parameter passing) that aren't widely supported.
provide xml schema definitions for all your types
one common problem in today's frameworks is their limitied ability to import multiple xml schema and wsdl documents. it's always a good idea to provide complete xml schema and wsdl definitions in one wsdl file rather than importing them from various locations. specifically, the microsoft .net framework is sensitive to xml schema import functions.
example: when you find that you need to include multiple wsdl and xml schema files in a .net service, you'll find it easier to pass the xml schema definitions together with the wsdl definition to the commandline ms .net wsdl compiler rather than trying to import the schemas into the wsdl file:
wsdl.exe /language:cs /protocol:soap myservice.wsdl myschema.xsd javacollections.xsd multiple wsdl bindings
some frameworks (e.g., ms .net) generate multiple wsdl bindings that allow you to access a web service through multiple protocols (e.g., http get, http post, and soap). the client-side framework needs to know which binding to use, so we need to specify further parameters (usually the fully qualified name of the service and the name of the wsdl port) when generating the programming language bindings from the wsdl document.
example: we will later run the com.systinet.demos.weather.weatherclient java client to access the ms .net weather service that is available on the xmethods site. notice that the client must use a slightly different lookup method to specify a fully qualified service name (as a javax.wsdl.qname) and wsdl port name: lookup("http://www.vbws.com/services/weatherretriever.asmx?wsdl",
new javax.wsdl.qname("http://tempuri.org/",
"weatherretriever"), "weatherretrieversoap", weatherretrieversoap.class)
default document style with literal encoding
some web services frameworks, including ms .net, generate, by default, a document style web service, using literal encoding. although the web service uses document/literal, the .net framework makes the service appear to be rpc style to .net clients. this isn't necessarily so for other web service frameworks, and many users may find it difficult to access the web service using an rpc style client. so if you're writing an rpc web service, force your framework to generate the rpc style wsdl.
example: use the [soaprpcservice] directive in your ms .net rpc web service implementations:
<%@ webservice language="c#" class="msnetstockservice" %>
using system.web.services;
using system.web.services.protocols;
using system.web.services.description;
[soaprpcservice]
public class msnetstockservice {
[webmethod]
public double getquote(string symbol) {
if(symbol == "sunw") {
return 10;
}
if(symbol == "beas") {
return 11;
}
if(symbol == "msft") {
return 50;
}
return 0;
}
}
use unique soapactions for your methods
if you start your web service development with a wsdl document definition, consider using unique soapaction attributes for all your methods in the wsdl binding. some frameworks rely on soapaction when routing soap messages to the web service implementation's methods.
example: consider the following wsdl binding fragment for the previously mentioned ms .net web service:
<binding name="msnetstockservicesoap" type="tns:msnetstockservicesoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
<operation name="getquote">
<soap:operation soapaction="http://tempuri.org/getquote" style="rpc" />
<input>
<soap:body use="encoded" namespace="http://tempuri.org/"
encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" />
</input>
<output>
<soap:body use="encoded" namespace="http://tempuri.org/"
encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" />
</output>
</operation>
</binding>
code examples
now we'll show a few web services interoperability examples. we will use a simple banking application that is capable of keeping track of customers, accounts, and account balances. we will also use the ms .net weather service that is listed on the xmethods site as a real-life example of internet web service access, so you'll need to be connected to the internet while running this demo.
note: if you haven't already downloaded the software used to create the tutorial examples, please refer to the installation chapter in part one. you'll also need to download the demo sources. we assume that you've unpacked this archive into the c:/wasp_demo directory. all java sources mentioned in the tutorial examples can be found in the src subdirectory of the unpacked demo sources archive. they all reside in the com.systinet.demos package. similarly all scripts used in the demo are located in the bin subdirectory. you don't need to download and use the software to understand these articles, but we strongly recommend it.
accessing a microsoft .net service from java
we'll start with the java client/.net service example. as we mentioned earlier, we will access the weather service listed on the xmethods site. click on the link and try the service online using the ms .net invocation framework. next look at the wsdl document for this service. you'll notice that it defines three bindings (weatherretrieversoap, weatherretrieverhttpget, and weatherretrieverhttppost).
now let's create a java client for this service. run the run_weather_client.bat script that's located in the bin directory of the demo sources archive. please note that we're using a lookup method that specifices the fully qualified service name and the wsdl port. this lookup method allows us to specify that we want to use the soap binding. lookup("http://www.vbws.com/services/weatherretriever.asmx?wsdl",
new javax.wsdl.qname("http://tempuri.org/",
"weatherretriever"), "weatherretrieversoap", weatherretrieversoap.class)
calling a java service from a microsoft .net client
the opposite scenario is pretty straightforward. first start the web service runtime with the startserver.bat command, and then compile and deploy our simple bank web service by invoking the deploy.bat command. now we're ready to show three different ms soap clients accessing our service.
note: you need to use ms ie 6.0 or higher to run this demo.
first we will run a ms javascript soap client. invoke the run_web_client.bat script. specify an ssn number (you can use 001-0001-01, 002-0002-02 or 003-0003-03), and click the get accounts button, which populates the accounts combobox. choose an account in the combobox and retrieve the account balance by clicking the get balance button. look at the javascript code in src/msjsclient.html for further details. the code is very self-explanatory.
note: you'll need to download and install the ms soap toolkit 2.0 in order to successfully complete the next example. you'll also need ms excel on your machine.
now let's see the same functionality accessed from microsoft excel (using visual basic). open the src/bank.xls spreadsheet and use the same steps described in the previous example to navigate the application. you can find the soap client code in the visual basic editor (tools->macro->visual basic editor) in the sheet1 module.
note: you'll need to download and install the ms .net framework sdk to run the c# part of this example
the last example demonstrates how to access our java web service from a c# client. run the run_csharp.bat script to generate a c# static proxy from the service wsdl and to compile and run the client application. this example is going one step further than our previous examples in that it passes a transfer structure from c# to java. the previous examples pass only simple data types. notice the simple xml schema definition of the transfer type (it includes the balance element):
<xsd:schema targetnamespace="http://idoox.com/wasp/tools/java2wsdl/output/com/systinet/demos/bank/">
<xsd:complextype name="transfer">
<xsd:sequence>
<xsd:element name="type" type="xsd:string"/>
<xsd:element name="accountnumberbeneficiary" type="xsd:string"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:element name="balance" type="ns0:balance"/>
<xsd:element name="amount" type="xsd:double"/>
<xsd:element name="timestamp" type="xsd:datetime"/>
<xsd:element name="accountnumber" type="xsd:string"/>
<xsd:element name="transferid" type="xsd:long"/>
<xsd:element name="commentbeneficiary" type="xsd:string"/>
</xsd:sequence>
</xsd:complextype>
<xsd:complextype name="balance">
<xsd:sequence>
<xsd:element name="changed" type="xsd:datetime"/>
<xsd:element name="transfer" type="ns0:transfer"/>
<xsd:element name="comment" type="xsd:string"/>
<xsd:element name="balance" type="xsd:double"/>
<xsd:element name="accountnumber" type="xsd:string"/>
<xsd:element name="transferid" type="xsd:long"/>
</xsd:sequence>
</xsd:complextype>
</xsd:schema>
the .net wsdl compiler will generate the appropriate structures in c#. see the stub in the src/ms/javaservice.cs for more details. run the java client by invoking the run_java_client.bat script, and you'll notice that it does the same thing.
note: both clients share the same web service, and both clients make withdrawals from the same account. so if you run the demo multiple times, you can easily get out of funds. then you'll get the transferprocessingexception that says something like 'insufficient funds'. one nice thing you'll notice is that c# is able to transparently rethrow the exception with the same message on the client side. restart the web services runtime using the startserver.bat to renew your funds.