Axis2开发webservice详解
?Axis2
是
Apache
提供的一款第三方
Web?Services
引擎,与其前身
Apache?Axis
相比,
axis2
更加高效、模块化,也更面向于
XML
的组织。经过精心的设计,
axis2
提供了更加便利的模块添加功能。??基于新的体系结构进行了全新编写,而且没有采用?
Axis?1.x?
的常用代码。支持开发?
Axis2?
的动力是探寻模块化更强、灵活性更高和更有效的体系结构,这种体系结构可以很容易地插入到其他相关?
Web?
服务标准和协议(如?
WS-Security
、
WS-ReliableMessaging?
等)的实现中。
Apache?Axis2?
是
Axis
的后续版本,是新一代的
SOAP
引擎。?
Web?Service
是现在最适合实现
SOA
的技术,而
Axis2
是目前实现
Web?Service
的一种相对比较成熟和流行的技术框架(架构)。下面我们就一起来看一下有关
axis2
在
webservice
开发的主要流程步骤。
在使用之前当然需要先安装axis2相关的服务?,安装axis2服务之前当然要先下载相关安装文件。下载地址: http://www.apache.org/dist//axis/axis2/java/core/1.6.1/ 在下载相关文件之前我们先来说一下有关于axis2的包?:
1.下载axis2?的war包。 2.解压所下载的war包到tomcat目录下的webapps下。 3.启动tomcat。在webapps目录下会生成axis2文件夹和相关文件。 访问http://localhost:8080/axis2/能看到以下页面表示axis2运行成功。 ? ? ? ? ? ? ? ? ? ?? Ok,安装完毕,简单吧。 安装完axis2之后。下一步我们就可以借用它来开发我们的webservice程序了。 1.首先创建一个web?project 2.创建服务类Helloworld
[java]
view plain
copy
print
?
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,内容如下:
[java]
view plain
copy
print
?
<?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文件件拷至HelloWorld下的WEB-INF目录下。把lib下的如下jar包也拷过去。然后在services下新建HelloWorld/META-INF路径,META-INF下新建services.xml,内容如下:
[html]
view plain
copy
print
?
<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? 能看到服务信息了。 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? 下面我们就可以写一个客户端来调用我们写的服务程序了。
[java]
view plain
copy
print
?
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(); } } } 运行结果: 各类的详细运用都在注释里面解释了。我想我就没必要在重复说了。注意参数不要配置错误 推荐优秀axis2讲解的博客:http://www.voidcn.com/article/p-fllmhdtr-bkp.html? 参考资料:http://blog.csdn.net/llhhyy1989/article/details/8312918 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??http://paggywong.iteye.com/blog/1350448 转自:http://www.voidcn.com/article/p-nlffpjrr-bcy.html (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |