一、前期准备
首先需要下载axis2的相关jar包,到axis的官方网站即可获得开发的依赖包:
下载地址:http://axis.apache.org/axis2/java/core/download.cgi
axis2-1.6.2-bin.zip里有axis2的jar包。
axis2-1.6.2-docs.zip里有详细的帮助文档。
axis2-1.6.2-src.zip里是源代码。
axis2-1.6.2-war.zip里是axis的管理平台,用于将WebService发布到Web容器中。放到tomcat可直接使用。将axis2-1.6.2-war.zip文件解压到相应的目录,将目录中的axis2.war文件放到<Tomcat安装目录>\webapps目录中,并启动Tomcat,在浏览器地址栏中输入如下的URL:
http://localhost:8080/axis2/,如看到axis2的主页面则安装成功。这是最简单的方法,但对我们学习没有帮助,当然这里我们不这么做
下载axis2-1.6.2-bin.zip,解压后的axis2-bin文件目录结构如下:
- bin文件夹是axis2的常用工具,其中有将wsdl文件转换成客户端调用的wsdl2java工具及将java转换成wsdl文件的工具
- conf是axis2的配置文件
- lib运行所要的依赖库
- repository是发布过的axis服务和文件
- sample是示例
- webapp是web文件和jsp页面等
二、编写和发布WebService
用Axis2实现Web Service,虽然可以将POJO类放在axis2\WEB-INF\pojo目录中直接发布成Web Service,这样做不需要进行任何配置,但这些POJO类不能在任何包中。这样有很多局限性,所以我们不这么做,Axis2可通过services.xml配置方式允许将带包的POJO类发布成Web Service。
Axis2所需的jar包(下面的Demo中提供下载)
创建web project项目,将所需jar包(在axis2-bin下lib目录中都可以找到)拷贝到WEB-INF/lib下,Add to Build Path .以下jar包都是必须的:
注意:圈出来的jar包,如果没有这个jar包,会报错:org.apache.axis2.AxisFault: Namespace URI may not be null ,并且很难找到原因,总之不要忽略它
写webservice 类
package com.april.webservice.server; public class WebServiceServer { public String sayHello(String param) { //... return "Hello, "+param; } }
使用services.xml配置文件发布
-
services.xml配置文件如下
<service name="helloService"> <description>Web Service例子</description> <parameter name="ServiceClass"> com.april.webservice.server.WebServiceServer </parameter> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </messageReceivers> </service>
- services.xml配置文件须放在META-INF目录下,<目录结构你可以自定义>在WEB-INF目录下创建services/firstService/META-INF文件夹,用于存放每个ws的services.xml
- 将解压的axis2-bin文件webapp目录下的axis2-web文件夹拷贝到WebRoot下。axis2-web这个文件夹是包含axis2整个的管理界面,可有可无,不过这个管理界面可以方便的查看所有可用的webservice以及每个ws中提供的方法
- services.xml中可以定义多个service
<serviceGroup> <service name="helloService1"> ... </service> <service name="helloService2"> ... </service> </serviceGroup>
-
web项目中整合嵌入webservice
因为axis2嵌入了web project,所以ws就不需要打包成aar,直接在/WEB-INF目录下创建相应的文件夹和services.xml,当然还必须在web.xml中配置axis2 servlet
<servlet> <servlet-name>AxisServlet</servlet-name> <display-name>Apache-Axis Servlet</display-name> <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
通过axis2的管理界面查看
如果在WebRoot下添加了axis2-web这个文件夹,将web项目加入到tomcat webapp中,启动,浏览器输入http://localhost:8080/webserviceDemo/axis2-web/就可以查看已经发布可用的webservice了
//wsdl2java简化客户端的编写
三、编写客户端调用程序
public static void main(String[] args) throws AxisFault { //本机tomcat端口默认为8081 EndpointReference targetEPR = new EndpointReference("http://localhost:8081/webserviceDemo/services/helloService"); RPCServiceClient sender = new RPCServiceClient(); Options options = sender.getOptions(); options.setTimeOutInMilliSeconds(2*20000L);//超时时间20s options.setTo(targetEPR); QName qname = new QName("http://server.webservice.april.com", "sayHello"); String str = "April"; Object[] param = new Object[]{str}; Class<?>[] types = new Class[]{String.class}; //这是针对返值类型的 /** * RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。 * invokeBlocking方法有三个参数 * 第一个参数的类型是QName对象,表示要调用的方法名; * 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[]; * 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。 * * 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。 */ Object[] response = sender.invokeBlocking(qname, param, types); System.out.println(response[0]); }
四、Demo下载
[下载Demo] 注:
- demo实例中包含以上配置、客户端RPC方式调用代码及所有Axis2必须的最小jar包
- 客户端调用程序被放在了webservice.client.WebServiceClient.java中,当然src/test也有一份
添加三行代码:org.apache.commons.httpclient.ConnectionPoolTimeoutException: Timeout waiting for connection at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.doGetConnection(MultiThreadedHttpConnectionManager.java:490) at org.apache.commons.httpclient.MultiThreadedHttpConnectionManager.getConnectionWithTimeout(MultiThreadedHttpConnectionManager.java:394) at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:152) at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:396) at org.apache.axis2.transport.http.AbstractHTTPSender.executeMethod(AbstractHTTPSender.java:621) at org.apache.axis2.transport.http.HTTPSender.sendViaPost(HTTPSender.java:193) at org.apache.axis2.transport.http.HTTPSender.send(HTTPSender.java:75) at org.apache.axis2.transport.http.CommonsHTTPTransportSender.writeMessageWithCommons(CommonsHTTPTransportSender.java:404) at org.apache.axis2.transport.http.CommonsHTTPTransportSender.invoke(CommonsHTTPTransportSender.java:231) at org.apache.axis2.engine.AxisEngine.send(AxisEngine.java:443) at org.apache.axis2.description.OutInAxisOperationClient.send(OutInAxisOperation.java:406) at org.apache.axis2.description.OutInAxisOperationClient.executeImpl(OutInAxisOperation.java:229) at org.apache.axis2.client.OperationClient.execute(OperationClient.java:165) at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:555) at org.apache.axis2.client.ServiceClient.sendReceive(ServiceClient.java:531) at org.apache.axis2.rpc.client.RPCServiceClient.invokeBlocking(RPCServiceClient.java:102)
options.setManageSession(true); options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true); //... Object[] response = sender.invokeBlocking(qname, param, types) sender.cleanupTransport(); //释放连接资源 System.out.println(response[0]);