Axis2开发webservice
转自http://blog.csdn.net/a491057947/article/details/19356731 1、安装axis2需要的文件下载地址 http://axis.apache.org/axis2/java/core/download.cgi2、安装axis2
1)下载axis2的war包
2)解压所下载的war包到tomcat目录下的webapps下
3)启动tomcat。在webapps目录下会生成axis2文件夹和相关的文件。访问http://localhost:8080/axis2可以看到访问成功的页面
3、开发webservice程序
1)首先创建一个web Project
2)创建服务类HelloWorld
package com.bzu.csh; public class HelloWorld { public String getHello(String name) { return "Hello, " + name + "."; } public String getWorld(String name) { return "World," + name + "."; } public String getHelloWorld() { return "Hello,World"; }
}
3)修改web.xml文件,内容如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee? http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> ? ? <!-- 加载Axis --> <servlet> <servlet-name>AxisServlet</servlet-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> </web-app>
4)把tomcat安装目录下的webapps/axis2/WEB-INF下的modules、service和conf文件拷贝到HelloWord下的WEB-INF目录下。把lib下的jar包也拷贝过去。然后在services下新建HelloWorld/META-INF路径,META-INF下新建services.xml,内容如下:
<service name="HelloWorld"> ?
? ? <description> ? ? ? ? ? HelloWorld Service Example ? ? </description> ? ? ? <parameter name="ServiceClass"> ? ? ? ? ? com.bzu.csh.HelloWorld ? ? </parameter> ? ? ? <operation name="getHello"> ? ? ? ? ? <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> ? ? ? </operation> ? ? ? <operation name="getWorld"> ? ? ? ? ? <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> ? ? ? </operation> ? ? ? <operation name="getHelloWorld"> ? <!-- 这里要注意,当没有返回值时才用? org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver,没有参数还是用RPCMessageReceiver--> ? ? ? ? <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> ? ? ? </operation> ? </service>
service.xml的配置详解:
1、<service name="HelloWorld">这里指定服务名称
2、<description>服务描述
3、<parameter name="ServiceClass">服务级参数
在services.xml文件中,我们可以直接在service节点下定义参数,这些参数供消息上下文(在运行时)、AxisService或者AxisOperation访问。参数有一个必选参数和可选参数:参数名称是必选参数。这里的服务参数为指定服务类。
4、<operation name="sayHello">
服务级消息接收器 ????Axis2中消息接收器是特殊的处理器,是In路径(请求路径)中的最后一个处理器。Web服务中的每个操作都有他自己的消息接收器,而且不同的操作可以有不同的消息接收器。消息接收器是依赖于消息交换模式的,所以我们必须为不同的消息交换模式指定不同的消息接收器。 怎样才能给所有的操作指定相同的消息接收器呢?只要添加服务级消息接收器即可。如此我们就不必在操作级别指定消息接收器了。我们要做的是指定服务级消息接收器。而在部署时,Axis2会自动给操作选择正确的消息接收器。这里我们指定 Operation?级消息接收器 ????前文描述了如何指定服务级消息接收器。但是,我们也可以为不同的操作指定不同的消息接收器,这需要在operation中指定messageReceiver标签 最后说明一个编写用于部署服务组的services.xml文件的问题 要在单个服务包文件中部署多个服务,服务组是一个便捷方法。当然,这些服务之间应该存在逻辑关系。用于服务组的services.xml文件和用于单个服务的,它们之间唯一的区别就是根元素。用于服务组的,根元素是serviceGroup,我们可以在serviceGroup元素内部定义多个service元素。 <serviceGroup> <service?name=service1> ...... <service> <service?name=service2> .......... </service> </serviceGroup> 启动tomcat后访问http://127.0.0.1:8080/Axis2Demo/services/HelloWorld?wsdl? 能看到服务信息了。 package com.bzu.client; import javax.xml.namespace.QName; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class ClientTest { public static void main(String[] args) { String url = "http://127.0.0.1:8080/Axis2Demo/services/HelloWorld"; String result = null; try { // 使用RPC方式调用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定调用WebService的URL EndpointReference targetEPR = new EndpointReference(url); options.setTo(targetEPR); // 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值 // // 指定要调用的getWorld方法及WSDL文件的命名空间..... QName opAddEntry = new QName("http://csh.bzu.com","getWorld"); //? // 指定getGreeting方法的参数值,如果有多个,继续往后面增加即可,不用指定参数的名称 Object[] opAddEntryArgs = new Object[] { "java" }; // 返回参数类型,这个和axis1有点区别 // invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名; // 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[]; // 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。 // 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{} // 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法, // 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同 // 指定getGreeting方法返回值的数据类型的Class对象..... Class[] classes = new Class[] { String.class }; // 调用getGreeting方法并输出该方法的返回值....... result = (String) serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,classes)[0]; System.out.println(result); // 下面是调用getHello方法的代码,这些代码与调用getWorld方法的代码类似 // classes = new Class[] {String.class}; opAddEntry = new QName("http://csh.bzu.com","getHello"); opAddEntryArgs = new Object[] { "曹胜欢" }; System.out.println(serviceClient.invokeBlocking(opAddEntry,classes)[0]); // 下面是调用getHelloWorld方法的代码 opAddEntry = new QName("http://csh.bzu.com","getHelloWorld"); System.out.println(serviceClient.invokeBlocking(opAddEntry,new Object[]{},classes)[0]); } catch (Exception e) { e.printStackTrace(); } } }(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |