WebService 简介
实际开发中,很多系统都是基于历史遗留系统进行开发,有时,这些系统基于不同的语言,如C,C++,C#,java,PHP等等。为了实现历史系统的再利用,或向外部程序暴露调用接口,于是就产生了WebService规范。WebService用于实现系统的透明访问,其最终目的:实现异构系统间通信。
单从Java方面考虑,实现WebService的方法有jdk1.6版本添加的JWS,Axis1/2,XFIRE,Celtix,CXF(Xfire和Celtix的整合)。
综合来看Axis1,Xfire,Celtix成熟但即将退出的版本。内置的JWS,Axis和CXF却也不是很成熟(哎,青黄不接)。
Apache Axis1实现WebService
当利用Java版Axis有两个途径,以Java码作为Web服务。最简单的一种是使用原生的jws(Java Web Service的文件)。另一种方法是使用定制的部署文件。定制部署让您能自选资源,什么要予以曝光。
第一种实现方式:即时发布
实现此功能需要借助 axis-1_4\webapps下的axis工程,将其部署到apache服务器webapps目录下。
原生的jws和java源代码的唯一区别是后缀名,当然采用这种方式也不能使用package进行打包.下面提供简单的原生jws和远程调用实现。
Hello.jws内容:
public class Hello
{
public String sayHello(String name)
{
StringBuffer greeting = new StringBuffer("Hello ");
if ( name != null )
{
greeting.append(name);
}
greeting.append(", nice to see you!");
return greeting.toString();
}
}
将Hello.jws文件放到apache服务器
webapps/axis/目录下,启动服务器。
通过 http://localhost:8080/axis/Hello.jws?wsdl 可以获得Hello.jws的WebService定义,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://localhost:8080/axis/Hello.jws"
xmlns:apachesoap="http://xml.apache.org/xml-soap"
xmlns:impl="http://localhost:8080/axis/Hello.jws"
xmlns:intf="http://localhost:8080/axis/Hello.jws"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
<wsdl:message name="sayHelloResponse">
<wsdl:part name="sayHelloReturn" type="xsd:string"/>
</wsdl:message>
<wsdl:message name="sayHelloRequest">
<wsdl:part name="name" type="xsd:string"/>
</wsdl:message>
<wsdl:portType name="Hello">
<wsdl:operation name="sayHello" parameterOrder="name">
<wsdl:input message="impl:sayHelloRequest" name="sayHelloRequest"/>
<wsdl:output message="impl:sayHelloResponse" name="sayHelloResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="HelloSoapBinding" type="impl:Hello">
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="sayHelloRequest">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/