AXIS2发布WebService
来源:互联网 发布:laylive客服系统源码 编辑:程序博客网 时间:2024/06/05 22:56
第一步:首先要下载开发所需要的jar包
下载:axis2-1.7.7-war.zip
http://axis.apache.org/axis2/java/core/download.html
下载完后解压至tomcat安装目录下的webapps文件夹下,启动tomcat后,在webapps目录下会生成axis2文件夹。
访问http://localhost:8080/axis2/能看到以下页面表示axis2运行成功。
第二步:
在Eclipse下新建Web Project,工程名:webService。新建包com.cm.service,新建类HelloWorld,代码如下:
package com.cm.service;public class HelloWorld {public String sayHello(String name){return "Hello, "+name+".";}public String saySorry(String name){return "Sorry,"+name+".";}public String getWorld(){return "Hello,World";}}
在WEB-INF目录下修改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"><!--Axis2 config start--><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> <!--Axis2 end--> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
把tomcat安装目录下的webapps/axis2/WEB-INF下的modules、service和conf文件件拷至HelloWorld下的WEB-INF目录下。把lib下的如下jar包拷过去。然后再services下新建HelloWorld/META-INF路径,META-INF下新建services.xml,内容如下:
<service name="HelloWorld"> <description> HelloWorld Service Example </description> <parameter name="ServiceClass"> com.cm.service.HelloWorld </parameter> <operation name="sayHello"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="saySorry"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </operation> <operation name="getWorld"> <messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> </operation> </service>
启动tomcat后访问http://127.0.0.1:8080/webServe/services/HelloWorld?wsdl能看到服务信息了。
到此Axis2的WebService服务已成功发布
Axis2客户端调用:
下面看看利用axis2 客户端调用实例
package com.cm.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 AxisUtil {public static void main(String[] args) {String xmlStr="xiaoxu.wang";String url="http://127.0.0.1:8080/webService/services/HelloWorld";String method="saySorry";AxisUtil.sendService(xmlStr,url,method);}public static String sendService(String xmlStr,String url,String method){String xml=null;try {RPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();EndpointReference targetEPR = new EndpointReference(url);options.setTo(targetEPR);// 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值 QName opAddEntry = new QName("http://service.cm.com",method); // 参数,如果有多个,继续往后面增加即可,不用指定参数的名称 Object[] opAddEntryArgs = new Object[] {xmlStr}; // 返回参数类型,这个和axis1有点区别 // invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名; // 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[]; // 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。 // 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{} // 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法, // 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同 Class[] classes = new Class[] { String.class }; xml=(String)serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]; System.out.println(xml); } catch (Exception e) {e.printStackTrace();long end = System.currentTimeMillis();}return xml;}}
运行结果:
Sorry,xiaoxu.wang.
总结:以上就是Axis2 服务发布与调用的简单案例。至于其他的实现方法有兴趣的可以继续研究。
附:源码,http://download.csdn.net/download/binyulong/10145064
- AXIS2:如何发布webservice
- AXIS2发布WebService
- [转载] AXIS2发布WebService
- axis2发布WebService工程
- Axis2发布WebService
- axis2发布webservice记录
- axis2发布webservice 记录
- axis2 发布webservice
- AXIS2:如何发布webservice .
- AXIS2:如何发布webservice .
- Axis2发布webservice
- Axis2 发布webservice
- AXIS2:如何发布webservice
- AXIS2:如何发布webservice
- AXIS2:如何发布webservice .
- Eclipse + Axis2发布WebService
- Axis2发布WebService服务
- axis2发布webservice
- JAVA | 49
- 二叉树的有关操作
- Numpy学习——数组填充np.pad()函数的应用
- 关于后台问题的一些思考
- Altium Designer高级功能初探之——覆铜规则
- AXIS2发布WebService
- Android语言切换的源码分析
- 单例模式和多线程
- 异或运算
- peak-linux-driver编译问题,无法找到popt.h
- winsock
- J2EE规范
- <转载>web开发--helloworld
- 总结函数的调用过程