第一部分: 安装Axis
第一步: 下载Axis
从Apache网站下载Axis发布包:
http://ws.apache.org/axis/
我下载的是
Axis 1.4 Final 版
第二步: 解压Axis压缩包
把下载的axis-bin-1_4.zip包解压缩到一个文件夹,可以是任意的,但如果文件夹包含中文名时,在中间有些过程需要对中文进行一些处理,否则可能出错;
我把axis-bin-1_4.zip解压到 F:\AXIS_Study,解压后的目录结构是这样的:
F:\AXIS_Study
|
|____axis-bin-1_4
|
|-----docs (文件夹)
|
|-----lib (文件夹)
|
|-----samples (文件夹)
|
|-----webapps (文件夹)
|
|-----xmls (文件夹)
|
|-----LICENSE
|
|-----NOTICES
|
|-----README
|
|-----release-notes.html
第三步:准备Web Server
准备一个Web Server,基本上所有主流的Web Server都可以,为了节省资源,我使用Tomcat5.5.23来讲解这个例子,大家可以自行到Apache网站上下载该版本的Tomcat。
第四步: 配置Tomcat
进入Tomcat目录,在conf/Catalina/localhost目录下新建一个XML文件,命名为axis.xml,然后在文件中输入如下内容:
- <? xml version = "1.0" encoding = "gb2312" ?>
- <!--
- Axis学习工程
- -->
- < Context docBase = "F:/AXIS_Study/axis-bin-1_4/webapps/axis" >
- </ Context >
这个文件是用来在Tomcat中配置一个Web应用的,这不在本次课程的范围,大家可以自行找相关资料来看。注意在文件中
docBase="F:/AXIS_Study/axis-bin-1_4/webapps/axis"
是指向Axis解压目录下面的webapps/axis目录,大家根据自己的目录结构自行修改
第五步:启动Tomcat
进入到Tomcat安装目录下的bin目录,找到startup.bat并执行之,在启动过程中,控制台窗口应该会出现以下信息:
2008-5-15 14:45:55 org.apache.catalina.core.AprLifecycleListener lifecycleEvent
信 息: The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: D:\JAVATools\jdk1.5.0
_05\bin;.;C:\windows\system32;C:\windows;D: \tools\系统\UnxUtils\bin;D:\tools\系统\UnxUtils\usr\local\wbin;D:\JAVATools \jdk1.5.0_05\bin;C:\oracle92\bin;C:\Progra
m Files\Oracle\jre\1.3.1\bin;C:\Program Files\Oracle\jre\1.1.8\bin;C:\windows\system32;C:\windows;C:\windows \System32\Wbem;D:\tools\系统;D:\JAVATools\ant-1.6.2\
bin
2008-5-15 14:45:55 org.apache.coyote.http11.Http11BaseProtocol init
信息: Initializing Coyote HTTP/1.1 on http-80
2008-5-15 14:45:55 org.apache.catalina.startup.Catalina load
信息: Initialization processed in 859 ms
2008-5-15 14:45:55 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2008-5-15 14:45:55 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/5.5.23
2008-5-15 14:45:55 org.apache.catalina.core.StandardHost start
信息: XML validation disabled
- Unable to find config file. Creating new servlet engine config file: /WEB-INF/server-config.wsdd
- Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
……
注意,最后面三行指示了Axis 的web app已经启动了,因为在我的Tomcat中发布的应用中,没有其他应用有使用Web Service,现在出现 .wsdd 文件,肯定就是Axis的应用启动了。
我们到
F:\AXIS_Study\axis-bin-1_4\webapps\axis\WEB-INF
目录下查看,发现系统自动生成了一个attachments目录,这与启动日志中的Attachment support is disabled.是对应的。
第六步:查看一下当前Axis中布署了哪些Web Service
打开浏览器,在地址栏输入:
http://localhost/axis/servlet/AxisServlet
浏览器显示结果:
And now... Some Services
AdminService (wsdl)
AdminService
Version (wsdl)
getVersion
这说明在Axis中已经布署了两个WebService,只不过这两个WebService都是Axis自带的,第一个是管理用的WebService,第二个是查看当前Axis版本信息的WebService。
大家可以点击相应的链接“wsdl”来看一下这两个WebService的 wsdl 文件。
结语:
至此,我们已经成功地把Axis 1.4 安装到Tomcat Web 服务器上了,接下来的目标是使用Axis来发布Web Service。
第二部分: 使用Axis发布Web Service
Axis提供两种方式将Java类发布成Web Services,这两种方式分别是:即时快速自动发布 和 通过配置文件进行发布,下面我们按顺序来讲如何使用这两种方式来发布WebService
即时快速自动发布:
第一步: 编写业务类
为了发布一个Web Service,首先必须要有业务类,为了节约时间,我写一个最简单的业务类:
- public class SayHello{
- private String name;
- public String hello(){
- return "Hello, axis Ver1.4 talking to you." ;
- }
- }
注意,采用快速自动发布的Web Service的业务类不能带 package,也不能实现任何接口(Interface),因为Axis自动发布时不支持Package。
第二步:把SayHello发布为Web Service
把SayHello.java复制到 axis目录下的webapps/axis目录下,然后把文件扩展名改为:jws
第三步:访问刚发布的Web Service
打开浏览器,在地址栏里输入:
http://localhost/axis/SayHello.jws
浏览器显示结果如下:
There is a Web Service here
Click to see the WSDL
这表示Web Service已经发布成功了。我们可以点击连接“Click to see the WSDL ”来看看生成的wsdl文件:
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < wsdl:definitions targetNamespace = "http://localhost/axis/SayHello.jws" xmlns:apachesoap = "http://xml.apache.org/xml-soap" xmlns:impl = "http://localhost/axis/SayHello.jws" xmlns:intf = "http://localhost/axis/SayHello.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 = "helloResponse" >
- < wsdl:part name = "helloReturn" type = "xsd:string" />
- </ wsdl:message >
- < wsdl:message name = "helloRequest" >
- </ wsdl:message >
- < wsdl:portType name = "SayHello" >
- < wsdl:operation name = "hello" >
- < wsdl:input message = "impl:helloRequest" name = "helloRequest" />
- < wsdl:output message = "impl:helloResponse" name = "helloResponse" />
- </ wsdl:operation >
- </ wsdl:portType >
- < wsdl:binding name = "SayHelloSoapBinding" type = "impl:SayHello" >
- < wsdlsoap:binding style = "rpc" transport = "http://schemas.xmlsoap.org/soap/http" />
- < wsdl:operation name = "hello" >
- < wsdlsoap:operation soapAction = "" />
- < wsdl:input name = "helloRequest" >
- < wsdlsoap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "http://DefaultNamespace" use = "encoded" />
- </ wsdl:input >
- < wsdl:output name = "helloResponse" >
- < wsdlsoap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "http://localhost/axis/SayHello.jws" use = "encoded" />
- </ wsdl:output >
- </ wsdl:operation >
- </ wsdl:binding >
- < wsdl:service name = "SayHelloService" >
- < wsdl:port binding = "impl:SayHelloSoapBinding" name = "SayHello" >
- < wsdlsoap:address location = "http://localhost/axis/SayHello.jws" />
- </ wsdl:port >
- </ wsdl:service >
- </ wsdl:definitions >
这个文件结构跟我们以前用IDE生成的没什么两样。
我们再访问一下这个Web Service,看看结果对不对,在浏览器地址栏输入:
http://localhost/axis/SayHello.jws?method=hello
浏览器显示服务器返回的结果:
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < soapenv:Envelope xmlns:soapenv = "http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd = "http://www.w3.org/2001/XMLSchema" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" >
- < soapenv:Body >
- < helloResponse soapenv:encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" >
- < helloReturn xsi:type = "xsd:string" > Hello, axis Ver1.4 talking to you. </ helloReturn >
- </ helloResponse >
- </ soapenv:Body >
- </ soapenv:Envelope >
这里返回的是整个SOAP数据包
接头我们用Jbuilder来生成该Web Service的客户端代码:
打开Jbuilder,新建一个工程,然后增加Axis Web Service Client支持:
菜单: New --> Web Service(J2EE1.3) --> Axis Web Service Client Configuration
然后在Web Service设计器中选择“Import from URL”,其中的wsdl文件路径就输入“http://localhost/axis/SayHello.jws?wsdl”
Jbuilder 就自动帮我们生成了所有配置。然后再make这个web service,这时候JBuilder自动生成了所需要的全部代码,还包括一个JUnit测试类 SayHelloServiceTestCase,为了能更直观地看到结果,我们再写一个自己的测试类:
- public class SayHelloTest {
- public void test(){
- SayHelloSoapBindingStub binding;
- try {
- binding = (SayHelloSoapBindingStub) new SayHelloServiceLocator().getSayHello(); //取接口
- binding.setTimeout(60000 ); //设置超时时间
- String helloMsg = binding.hello(); //调用hello方法
- System.out.println(">>>>>>>>>>>>>>>>服务器返回消息:" );
- System.out.println( helloMsg );
- System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" );
- }catch (javax.xml.rpc.ServiceException ex) {
- if (ex.getLinkedCause() != null )
- ex.getLinkedCause().printStackTrace();
- System.out.println("JAX-RPC调用出错:" );
- ex.printStackTrace();
- } catch (RemoteException ex) {
- System.out.println("Web Service调用出错:" );
- ex.printStackTrace();
- }
- }
- public static void main(String[] args) {
- SayHelloTest sayhellotest = new SayHelloTest();
- sayhellotest.test();
- }
- }
编译,然后执行该类,得到输出结果是:
>>>>>>>>>>>>>>>>服务器返回消息:
Hello, axis Ver1.4 talking to you.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
至此,我们通过Axis自动发布的Web Service成功了。
=======================
通过配置文件(WSDD)进行发布:
第一步:先写一个配置文件 server-config.wsdd
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < deployment name = "defaultClientConfig"
- xmlns:java = "http://xml.apache.org/axis/wsdd/providers/java"
- xmlns:handler = "http://xml.apache.org/axis/wsdd/providers/handler" xmlns = "http://xml.apache.org/axis/wsdd/" >
- < globalConfiguration name = "defaultClientConfig" >
- < requestFlow name = "RequestFlow1" type = "" >
- < handler name = "Handler1" type = "java:org.apache.axis.handlers.JWSHandler" >
- < parameter name = "scope" value = "session" />
- </ handler >
- < handler name = "Handler2" type = "java:org.apache.axis.handlers.JWSHandler" >
- < parameter name = "scope" value = "request" />
- < parameter name = "extension" value = ".jwr" />
- </ handler >
- </ requestFlow >
- </ globalConfiguration >
- < handler name = "URLMapper" type = "java:org.apache.axis.handlers.http.URLMapper" />
- < handler name = "LocalResponder" type = "java:org.apache.axis.transport.local.LocalResponder" />
- < handler name = "Authenticate" type = "java:org.apache.axis.handlers.SimpleAuthenticationHandler" />
- < transport name = "http" type = "" >
- < requestFlow name = "RequestFlow1" type = "" >
- < handler name = "Handler1" type = "URLMapper" />
- < handler name = "Handler2" type = "java:org.apache.axis.handlers.http.HTTPAuthHandler" />
- </ requestFlow >
- </ transport >
- < transport name = "local" type = "" >
- < responseFlow name = "ResponseFlow1" type = "" >
- < handler name = "Handler1" type = "LocalResponder" />
- </ responseFlow >
- </ transport >
- <!--这里配置了一个Web Service,如果有多个Web Service,就按这种格式在下面增加即可-->
- < service name = "SayHello2" provider = "java:RPC" >
- < parameter name = "scope" value = "Request" />
- < parameter name = "className" value = "SayHello" />
- < parameter name = "allowedMethods" value = "*" />
- </ service >
- </ deployment >
需要注意的通过配置文件来发布Web Service时,业务类是可以有package的,这里为了利用上面的业务类,所以就没有package
然后把该配置文件放置到WEB-INF目录下。
第二步:编译业务类
把上面写的业务编译成class文件,放到WEB-INF/classes目录下
第三步:重启Tomcat
第四步: 查看新发布的Web Service
打开浏览器,地址栏里输入: http://localhost/axis/servlet/AxisServlet
浏览器返回的结果如下:
And now... Some Services
SayHello2 (wsdl)
hello
这表明我们的新Web Service发布成功了,我们可以进一步看一下wsdl文件:
- <? xml version = "1.0" encoding = "UTF-8" ?>
- < wsdl:definitions targetNamespace = "http://localhost/axis/services/SayHello2" xmlns:apachesoap = "http://xml.apache.org/xml-soap" xmlns:impl = "http://localhost/axis/services/SayHello2" xmlns:intf = "http://localhost/axis/services/SayHello2" 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 = "helloRequest" >
- </ wsdl:message >
- < wsdl:message name = "helloResponse" >
- < wsdl:part name = "helloReturn" type = "soapenc:string" />
- </ wsdl:message >
- < wsdl:portType name = "SayHello" >
- < wsdl:operation name = "hello" >
- < wsdl:input message = "impl:helloRequest" name = "helloRequest" />
- < wsdl:output message = "impl:helloResponse" name = "helloResponse" />
- </ wsdl:operation >
- </ wsdl:portType >
- < wsdl:binding name = "SayHello2SoapBinding" type = "impl:SayHello" >
- < wsdlsoap:binding style = "rpc" transport = "http://schemas.xmlsoap.org/soap/http" />
- < wsdl:operation name = "hello" >
- < wsdlsoap:operation soapAction = "" />
- < wsdl:input name = "helloRequest" >
- < wsdlsoap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "http://DefaultNamespace" use = "encoded" />
- </ wsdl:input >
- < wsdl:output name = "helloResponse" >
- < wsdlsoap:body encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/" namespace = "http://localhost/axis/services/SayHello2" use = "encoded" />
- </ wsdl:output >
- </ wsdl:operation >
- </ wsdl:binding >
- < wsdl:service name = "SayHelloService" >
- < wsdl:port binding = "impl:SayHello2SoapBinding" name = "SayHello2" >
- < wsdlsoap:address location = "http://localhost/axis/services/SayHello2" />
- </ wsdl:port >
- </ wsdl:service >
- </ wsdl:definitions >
第五步:写测试客户端
在Jbuilder中,刚才的工程里增加一个Web Service客户端,还是在Web Service设计器中选择“Import from URL”,其中的wsdl文件路径就输入“http://localhost/axis/services/SayHello2?wsdl”,然后 make这个web service module。同样的,我们也再写一个自己的测试类:
- public class SayHello2Test {
- public void test(){
- SayHello2SoapBindingStub binding;
- try {
- binding = (SayHello2SoapBindingStub) new SayHello2ServiceLocator().getSayHello(); //取接口
- binding.setTimeout(60000 ); //设置超时时间
- String helloMsg = binding.hello(); //调用hello方法
- System.out.println(">>>>>>>>>>>>>>>>服务器返回消息:" );
- System.out.println( helloMsg );
- System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" );
- }catch (javax.xml.rpc.ServiceException ex) {
- if (ex.getLinkedCause() != null )
- ex.getLinkedCause().printStackTrace();
- System.out.println("JAX-RPC调用出错:" );
- ex.printStackTrace();
- } catch (RemoteException ex) {
- System.out.println("Web Service调用出错:" );
- ex.printStackTrace();
- }
- }
- public static void main(String[] args) {
- SayHello2Test sayhellotest = new SayHello2Test();
- sayhellotest.test();
- }
- }
运行这个测试类,得到的输出结果是:
>>>>>>>>>>>>>>>>服务器返回消息:
你好,现在是 axis Ver1.4 在与你对话。