使用axis怎么做axis2 webservice接口口

博客分类:
所谓Web Service就是客户端以标准的SOAP消息将服务请求发给服务提供者,不论服务提供者用什么样的技术,Java、EJB、或者.NET执行相应的程序得到结果,然后将结果以SOAP消息返回给服务请求者。以axis为例,创建一个简单的Web Service 首先到上去下载最新的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目录下,如下列表:
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-1.jar
第二步:创建WebService服务器端接口和实现类
package com.unis.p2p.
* WebService服务器端业务逻辑接口
* @author Posey
public interface Hello {
* 接口方法:接收客户端WebService请求
* @author Posey
* @param message
public String executeTaskList(String message);
package com.unis.p2p.
* WebService服务器端业务逻辑实现类
* @author Posey
public class HelloImpl implements Hello {
* 接收客户端WebService请求,具体执行相关操作
* @author Posey
public String executeTaskList(String message) {
return "OK|调用成功! " +
第三步:修改WEB-INF/web.xml配置文件
&?xml version="1.0" encoding="UTF-8"?&
&web-app version="2.5" xmlns="/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee
/xml/ns/javaee/web-app_2_5.xsd"&
&!-- ********************************************** --&
&!-- WebService核心处理类 --&
&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服务。
地址栏输入:回车,如果没有错误提示,则恭喜你,你的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, :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." 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.
import java.net.URL;
import org.apache.axis.client.C
import org.apache.axis.client.S
import org.junit.T
* 类说明:Client端测试类,发出请求
* @author Posey
public class WebServiceTest {
public void testExecuteTask() throws Exception {
String res = "";
String ss = "ERR|35|8428";
String[] str = ss.split("\\|");
String tempMsg = "";
for (int i = 1; i & str. 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);
第七步:Run As - JUnit Test,执行成功输出结果:OK|调用成功! 35,8428
James_Posey
浏览: 5927 次
来自: 北京现在位置:
Axis1.4是Java语言的webservice实现之一。
调用代码案例:
package com.what21.axis14;
import java.net.URL;
import javax.xml.namespace.QN
import javax.xml.rpc.ParameterM
import org.apache.axis.C
import org.apache.axis.client.C
import org.apache.axis.client.S
import org.apache.axis.encoding.XMLT
import org.apache.log4j.L
public class SyncCallWS {
static final Logger logger = Logger.getLogger(SyncCallWS.class);
* webservice调用初始化
* @param wsdl
* @param username
* @param password
* @throws Exception
private static Call initCall(String wsdl,String username,String password)
throws Exception {
Service service=new Service();
Call call =
call = (Call) service.createCall();
call.setTargetEndpointAddress(new URL(wsdl));
// 使用SOAP Action响应
call.setUseSOAPAction(true);
// 超时时间设置
call.setProperty(Call.CONNECTION_TIMEOUT_PROPERTY, new Integer(60 * 60000));
call.setTimeout(new Integer(60 * 60000));
} catch (Exception e) {
("调用webservice接口出现异常!");
e.printStackTrace();
* @param url
* @param methodName
* @param userInfo
* @throws Exception
public static void addUser(String url)
throws Exception {
// 初始化call
Call call = initCall(url, "", "");
String result = "";
// 设置调用方法
call.setOperationName("addUserInfo");
call.setOperationName(new QName("addUser","addUserInfo"));
call.addParameter("userInfos", Constants.XSD_STRING, ParameterMode.IN);
// 返回字符类型
call.setReturnType(XMLType.XSD_STRING);
String param = "";
// 参数是数组,也就是参数个数
Object[] obj = new Object[]{ param };
result = (String) call.invoke(obj);
System.out.println(result);
* @param args
public static void main(String[] args) {
String url = "http://10.210.81.71:58045/pasm/ws";
addUser(url);
} catch (Exception e) {
e.printStackTrace();
生成的请求SOAP消息格式为:
&?xml version="1.0" encoding="UTF-8"?&
&soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&
&soapenv:Body&
&addUserInfo
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&
&userInfos xsi:type="xsd:string"&&/userInfos&
&/addUserInfo&
&/soapenv:Body&
&/soapenv:Envelope&
SOAP消息格式调整,请求方法需要带有前缀:
call.setOperationName("addUserInfo");
call.setOperationName(new QName("addUser","addUserInfo"));
&?xml version="1.0" encoding="UTF-8"?&
&soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&
&soapenv:Body&
&ns1:addUserInfo
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:ns1="addUser"&
&userInfos xsi:type="xsd:string"&&/userInfos&
&/ns1:addUserInfo&
&/soapenv:Body&
&/soapenv:Envelope&
call.setOperationName("addUserInfo");
call.setOperationName(new QName("addUserInfo"));
call.setOperationName(new QName("","addUserInfo"));
上述3行代码是等效的。
SOAP消息格式调整,消息体需要带有前缀:
call.addParameter("userInfos", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter(new QName("addUser","userInfos"), Constants.XSD_STRING, ParameterMode.IN);
&?xml version="1.0" encoding="UTF-8"?&
&soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&
&soapenv:Body&
&ns1:addUserInfo
soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:ns1="addUser"&
&ns1:userInfos xsi:type="xsd:string"&&/ns1:userInfos&
&/ns1:addUserInfo&
&/soapenv:Body&
&/soapenv:Envelope&
call.addParameter("userInfos", Constants.XSD_STRING, ParameterMode.IN);
call.addParameter(new QName("userInfos"), Constants.XSD_STRING, ParameterMode.IN);
call.addParameter(new QName("","userInfos"), Constants.XSD_STRING, ParameterMode.IN);
上述3行代码是等效的。基于JAVA中使用Axis发布/调用Webservice的方法详解
字体:[ ] 类型:转载 时间:
如果初识axis发布/调用WS,建议先读上面的参考文件,本文对于发布/调用WS的主要步骤只是简单文字描述,没有它写的详尽
本示例和参考文章的差别在于:
1)deploy.wsdd定义的更详细(对于server端定义了接口:ICalculate): 代码如下:&deployment xmlns="http://xml.apache.org/axis/wsdd/"&&& xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"&&&& &service name="Calculate" provider="java:RPC" style="rpc" use="literal"&&&&&&&& &parameter name="wsdlTargetNamespace" value="http://web.webservice.CalculateService.org" /&&&&&&&& &parameter name="wsdlServiceElement" value="Calculate" /&&&&&&&& &parameter name="wsdlServicePort" value="CalculateService" /&&&&&&&& &parameter name="wsdlPortType" value="ICalculate" /&&&&&&&& &parameter name="className" value="org.calculateservice.service.CalculateImp" /&&&&&&&& &parameter name="typeMappingVersion" value="1.2" /&&&&&&&& &parameter name="allowedMethods" value="add sub" /&&&&&&&& &parameter name="scope" value="Request" /&&&&&&&& &operation name="add" qname="operNS:add"&&&&&&&&&&& xmlns:operNS="http://web.webservice.CalculateService.org"&&&&&&&&&&& returnQName="addReturn" returnType="rtns:int" xmlns:rtns="http://www.w3.org/2001/XMLSchema"&&&&&&&&&&& soapAction=""&&&&&&&&&&&& &parameter qname="x" type="tns:int"&&&&&&&&&&&&&&& xmlns:tns="http://www.w3.org/2001/XMLSchema" /&&&&&&&&&&&& &parameter qname="y" type="tns:int"&&&&&&&&&&&&&&& xmlns:tns="http://www.w3.org/2001/XMLSchema" /&&&&&&&& &/operation&&&&&&&& &operation name="sub" qname="operNS:sub"&&&&&&&&&&& xmlns:operNS="http://web.webservice.CalculateService.org"&&&&&&&&&&& returnQName="subReturn" returnType="rtns:int" xmlns:rtns="http://www.w3.org/2001/XMLSchema"&&&&&&&&&&& soapAction=""&&&&&&&&&&&& &parameter qname="x" type="tns:int"&&&&&&&&&&&&&&& xmlns:tns="http://www.w3.org/2001/XMLSchema" /&&&&&&&&&&&& &parameter qname="y" type="tns:int"&&&&&&&&&&&&&&& xmlns:tns="http://www.w3.org/2001/XMLSchema" /&&&&&&&& &/operation&&&& &/service&&/deployment&2)自定义了AxisServlet:org.calculateservice.core.AxisServlet(兼容了.NET 去掉了SOAPACTION的检验);
详细代码这里不贴了,感兴趣自行下载完整示例代码;
3)调用方式为用本地类调用(java调用WS好像有三种方式,个人感觉用本地代理类调用最为合理和可读): 代码如下:public static void main(String[] args) throws ServiceException, RemoteException {&&&&&&& &&&&&&& Calculate calculate& = new CalculateLocator();&&&&&&& int result = calculate.getCalculateService().add(1, 2);&&&&&&& System.out.println("[%1 + 2 = " + result + "%]");&&& }发布/调用WS的主要步骤:
1)将下载资源中的axis1.4安装包中的webapp中的axis目录copy到tomcat中的webapp目录中;
2)编写WS服务端实现代码;
3)在WEB-INF目录编写deploy.wsdd部署文件;
4)编写:generate-server-config.bat脚本,然后生成:server-config.wsdd,然后发布webservice;
5)编写:wsdl2java.bat脚本生成本地调用client代码;
6)编写测试代码调用WS;
代码结构图:
client:用WSDL2Java根据wsdl生成本地的client代码;
core:自定义的AxisServlet,兼容了.NET 去掉了SOAPACTION的检验;
service:WS服务端实现代码(这里用一个简单的加减计算做为实例);
test:根据生成的client类去调用server(生成的代码server地址是localhost的,如果需要将此地址改为配置,修改CalculateLocator类中变量CalculateService_address赋值即可);
资源中包含示例完整代码和axis1.4安装文件
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具

我要回帖

更多关于 webservice axis2 的文章

 

随机推荐