6. 使用Axis开发WebService程序
转自:http://www.itkeyword.com/doc/7529577946427268306/Apache-Servlet-WebSOAPWebService
所谓Web Service就是客户端以标准的SOAP消息将服务请求发给服务提供者,不论服务提供者用什么样的技术,Java、EJB、或者.NET执行相应的程序得到结果,然后将结果以SOAP消息返回给服务请求者。以axis为例,创建一个简单的Web Service
首先到http://ws.apache.org/axis/上去下载最新的axis版本。最新版本是axis-src-1_4.zip
NEWS (April 22, 2006): Axis 1.4 Final is now available!
第一步:创建WebService项目,命名为Webservice_Begin
解压axis-src-1_4.zip后,拷贝lib目录下最基本的jar包至项目WEB-INF/lib目录下,如下列表:
axis.jar
commons-discovery-0.2.jar
commons-logging-1.1.jar
jaxrpc.jar
mailapi_1_3_1.jar
wsdl4j-1.5.1.jar
junit-4.9-SNAPSHOT-20100512-0041.jar
第二步:创建WebService服务器端接口和实现类
- package com.unis.p2p.server;
- /**
- * WebService服务器端业务逻辑接口
- *
- * @author Posey 2010-12-17
- */
- public interface Hello {
- /**
- * 接口方法:接收客户端WebService请求
- *
- * @author Posey 2010-12-17
- * @param message
- * @return
- */
- public String executeTaskList(String message);
- }
- package com.unis.p2p.server;
- /**
- * WebService服务器端业务逻辑实现类
- * @author Posey 2010-12-17
- *
- */
- public class HelloImpl implements Hello {
- /**
- * 接收客户端WebService请求,具体执行相关操作
- *
- * @author Posey 2010-12-17
- */
- public String executeTaskList(String message) {
- return "OK|调用成功! " + message;
- }
- }
第三步:修改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">
- <!-- ********************************************** -->
- <!-- WebService核心处理类 -->
- <servlet>
- <servlet-name>AxisServlet</servlet-name>
- <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>AxisServlet</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- <!-- ********************************************** -->
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
第四步:创建WebRoot/WEB-INF/server-config.wsdd文件,定义服务的名称,具体的实现类,以及发布的方法和属性等等
- <?xml version="1.0" encoding="UTF-8"?>
- <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">
- <handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper" />
- <service name="executeTask" provider="java:RPC">
- <parameter name="className" value="com.unis.p2p.server.HelloImpl" />
- <parameter name="allowedMethods" value="executeTaskList" />
- </service>
- <transport name="http">
- <requestFlow>
- <handler type="URLMapper" />
- </requestFlow>
- </transport>
- </deployment>
第五步:启动应用服务器,发布Web Service服务。
地址栏输入:http://127.0.0.1:8080/Webservice_Begin/services回车,如果没有错误提示,则恭喜你,你的Web Service已经发布成功。
点击wsdl,还可以看到具体的wsdl的配置信息如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- - <wsdl:definitions targetNamespace="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" xmlns:intf="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" 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="executeTaskListResponse">
- <wsdl:part name="executeTaskListReturn" type="soapenc:string" />
- </wsdl:message>
- - <wsdl:message name="executeTaskListRequest">
- <wsdl:part name="message" type="soapenc:string" />
- </wsdl:message>
- - <wsdl:portType name="HelloImpl">
- - <wsdl:operation name="executeTaskList" parameterOrder="message">
- <wsdl:input message="impl:executeTaskListRequest" name="executeTaskListRequest" />
- <wsdl:output message="impl:executeTaskListResponse" name="executeTaskListResponse" />
- </wsdl:operation>
- </wsdl:portType>
- - <wsdl:binding name="executeTaskSoapBinding" type="impl:HelloImpl">
- <wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
- - <wsdl:operation name="executeTaskList">
- <wsdlsoap:operation soapAction="" />
- - <wsdl:input name="executeTaskListRequest">
- <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://server.p2p.unis.com" use="encoded" />
- </wsdl:input>
- - <wsdl:output name="executeTaskListResponse">
- <wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" use="encoded" />
- </wsdl:output>
- </wsdl:operation>
- </wsdl:binding>
- - <wsdl:service name="HelloImplService">
- - <wsdl:port binding="impl:executeTaskSoapBinding" name="executeTask">
- <wsdlsoap:address location="http://127.0.0.1:8080/Webservice_Begin/services/executeTask" />
- </wsdl:port>
- </wsdl:service>
- </wsdl:definitions>
第六步:创建WebService客户端测试类
/Webservice_Begin/test/com/unis/p2p/client/WebServiceTest.java
- package com.unis.p2p.client;
- import java.net.URL;
- import org.apache.axis.client.Call;
- import org.apache.axis.client.Service;
- import org.junit.Test;
- /**
- * 类说明:Client端测试类,发出请求
- * @author Posey 2010-12-17
- *
- */
- public class WebServiceTest {
- @Test
- public void testExecuteTask() throws Exception {
- String res = "";
- String ss = "ERR|2352|5683|2235|8428";
- String[] str = ss.split("\\|");
- String tempMsg = "";
- for (int i = 1; i < str.length; i++) {
- tempMsg += str[i] + ",";
- }
- tempMsg = tempMsg.substring(0, tempMsg.length()-1);
- //调接口
- res = this.executeTask("http://127.0.0.1:8080/Webservice_Begin/services/executeTask",
- "executeTaskList",
- new Object[]{ tempMsg });
- System.out.println(res); //输出结果
- }
- //核心处理方法
- private synchronized String executeTask(String url, String method, Object[] args) throws Exception {
- // 创建Service实例
- Service service = new Service();
- // 通过Service实例创建Call实例
- Call call = (Call) service.createCall();
- // 将WebService的服务路径加入到Call实例中,并为Call设置服务的位置
- URL webServiceUrl = new URL(url);
- call.setTargetEndpointAddress(webServiceUrl);
- // 调用WebService方法
- call.setOperationName(method);
- // 调用WebService传入参数
- String result = (String) call.invoke(args);
- return result;
- }
- }
第七步:Run As - JUnit Test,执行成功
6. 使用Axis开发WebService程序的更多相关文章
- WebService应用--使用java开发WebService程序
使用Eclipse开发第一个WebService程序,本示例采用的工具为Spring-Tool-Suite,和Eclipse没有本质的区别,开发环境jdk1.7 一.开发步骤: 1.新建名为WebSe ...
- axis2开发webservice程序
一.环境 eclipse + jdk 6.0 + win7 64位 +tomcat7.0 二.创建服务端程序 1.新建web项目,webserviceTest 2.下载axis2,将lib目录下的ja ...
- JAX-WS开发WebService程序
近来公司里要用的到WebService做开发,所以就自己学习了一下,刚开始感觉挺难的,但是真正学会以后,原来这么简单. 今天把这些东西哦记下来,以便日后的复习. 我来介绍一下我的开发环境:Eclips ...
- struts2结合axis开发webservice
第一步:引入axis的依赖jar包 第二步:修改web.xml文件 <listener> <listener-class>org.apache.axis.transport ...
- 使用CXF开发WebService程序的总结(七):Spring+CXF+Mybatis+Mysql共同打造的服务端示例
通过该demo,可以 熟悉下 spring+cxf+maven+mybatis+mysql等常见后端技术整合 1. 在前面的 父工程 ws_parent 中 添加依赖 由于原来的项目是使用的cxf依赖 ...
- 使用CXF开发WebService程序的总结(六):结合拦截器使用
1. 使用CXF提供的拦截器 拦截器在我看来分为两端两向,两端分为:客户端和服务端,两向(方向)分为:进(in)和出(out),即大致四类拦截器. 在这里利用cxf提供的 日志拦截器举例 1.1 在服 ...
- 使用CXF开发WebService程序的总结(五):基于Map数据类型处理的的客户端和服务端代码的编写
1. 首先我们按照List或数组等处理方式来处理Map,看看效果 1.1 在服务端的接口中添加以下方法 /** * 查询所有班级信息加上对应的学生列表 * * @return */ public Ma ...
- 使用CXF开发WebService程序的总结(四):基于bean的客户端和服务端代码的编写
1. 在原服务端项目 ws_server中添加两个bean 1.1 添加两个类 User 和 Clazz package com.lonely.pojo; public class User { ...
- 使用CXF开发WebService程序的总结(三):创建webservice客户端
1.创建一个maven子工程 ws_client,继承父工程 1.1 修改父工程pom配置 <modules> <module>ws_server</module> ...
随机推荐
- django 之数据库模块
前提ajango的 数据库主要是为了存取网站的一些内容,数据库的设置一般放在model.py 下 目录下 我们设置如下的数据库:具体的代码如下面所示: # -*- coding: utf-8 -* ...
- 紫书 例题8-9 UVa 1451 (数形结合)
这道题用了数形结合, 真的牛逼, 完全想到不到还可以这么做 因为题目求的是平均值, 是总数除以个数, 这个时候就可以联系 到斜率, 也就是说转化为给你一堆点, 让你求两点之间的最大斜率 要做两个处理 ...
- javascript取前n天的日期两种方法
方法一: var d = new Date(); d = new Date(d.getFullYear(),d.getMonth(),d.getDate()-n); 方法二: var now = ne ...
- codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数
对于这道题目以及我的快速幂以及我的一节半晚自习我表示无力吐槽,, 首先矩阵乘法和快速幂没必要太多说吧,,嗯没必要,,我相信没必要,,实在做不出来写两个矩阵手推一下也就能理解矩阵的顺序了,要格外注意一些 ...
- HDU——T 1507 Uncle Tom's Inherited Land*
http://acm.hdu.edu.cn/showproblem.php?pid=1507 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- GridView中使用CheckBox
asp.net中checkbox是向server提交时才干触发事件 把该控件的autopostback设置为true.才干响应事件 protected void Checkbox_CheckedCh ...
- 动态语言切换(续)-designer中的retranslateUi(带源码)
本站所有文章由本站和原作者保留一切权力,仅在保留本版权信息.原文链接.原文作者的情况下允许转载,转载请勿删改原文内容, 并不得用于商业用途. 谢谢合作.原文链接:动态语言切换(续)-designer中 ...
- thinkphp5项目--个人博客(三)
thinkphp5项目--个人博客(三) 项目地址 fry404006308/personalBlog: personalBloghttps://github.com/fry404006308/per ...
- 安卓开发--HttpDemo02
package com.cnn.httpdemo02; import android.app.Activity; import android.os.Bundle; import android.vi ...
- BZOJ 2342 Manacher
思路: 首先用manacher可以求出以i和i+1中间为对称轴,最长回文串能扩增的长度p[i]. 然后4*(y-x)能更新答案,当且仅当y≤x+p[x]/2且y-p[y]≤x. 按i-p[i]将所有点 ...