微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

webservice客户端专题文档

1、 技术简介
Web Service主要是为了使原来各孤立的站点间的信息能够相互通信、共享而提出的一种接口。 Web Service所使用的是Internet上统一、开放的标准,如HTTP、XML、SOAP(简单对象访问协议)、WSDL等,所以Web Service可以在任何支持这些标准的环境(Windows,Linux)中使用。注:SOAP协议(Simple Object Access Protocal,简单对象访问协议),它是一个用于分散和分布式环境下网络信息交换的基于XML的通讯协议。在此协议下,软件组件或应用程序能够通过标准的HTTP协议进行通讯。它的设计目标就是简单性和扩展性,这有助于大量异构程序和平台之间的互操作性,从而使存在的应用程序能够被广泛的用户访问。


2、技术框架
3、分支及版本
4、客户端调用
4.1客户端axis1
4.1.1引入jar
axis.jar

commons-logging.jar

commons-discovery.jar

jaxrpc.jar;

 


4.1.2测试代码
package com;


import java.net.MalformedURLException;

import java.rmi.remoteexception;


import javax.xml.namespace.QName;

import javax.xml.rpc.ServiceException;


import org.apache.axis.client.Call;

import org.apache.axis.client.Service;


public class TestWebServiceClient {

public static void main(String[] args) throws Exception {

System.out.println(callService("aaa"));

}

public static String callService(String xmlMessage) throws Exception {

//创建服务请求对象

Service service = new Service();

Call call=null;

String res = "";

try {

call = (Call) service.createCall();

//如果要调用方法有命名空间,需要new QName(命名空间,方法名)

call.setoperationName(new QName("http://control.order.ab.com","receiveData"));

call.setTargetEndpointAddress(new java.net.URL("http://localhost:8080/services/testWebservice?wsdl"));//

//call.setoperationName("receiveData");// 这是要调用方法

res = (String) call.invoke(new Object[] { xmlMessage });

System.out.println(res);

} catch (ServiceException e) {

e.printstacktrace();

throw e;

} catch (MalformedURLException e) {

e.printstacktrace();

throw e;

} catch (remoteexception e) {

e.printstacktrace();

throw e;

}

return res;


}


}

4.2客户端axis2
4.2.1引入jar
axis2-adb-1.5.jar

axis2-kernel-1.5.jar

axiom-api-1.2.8.jar

commons-logging-1.0.4.jar

wsdl4j.jar

XmlSchema-1.4.3.jar

axiom-impl-1.2.8.jar

neethi-2.0.4.jar

axis2-transport-local-1.5.jar

axis2-transport-http-1.5.jar

commons-httpclient-3.1.jar

mail-1.4.jar

httpcore-4.1.2.jar

commons-codec-1.3.jar

 

4.2.2测试代码
package com;


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 TestWebServiceClient2 {

 

public static void main(String[] args) {

// Todo Auto-generated method stub

String reqXml="";


String respXml=sendService(reqXml,"http://10.10.139.32:8001/yeePayProxy/services/orderController?wsdl","http://control.order.ab.com","receiveData");

System.out.println(respXml);

}

public static String sendService(String xmlStr,String url,String namespace,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(namespace,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;


}


}

 

4.3客户端XFire
4.3.1引入jar包
<classpathentry kind="lib" path="lib/xfire/xfire-all-1.2.6.jar"/>

<classpathentry kind="lib" path="lib/xfire/commons-logging-1.0.4.jar"/>

<classpathentry kind="lib" path="lib/xfire/wsdl4j-1.6.1.jar"/>

<classpathentry kind="lib" path="lib/xfire/XmlSchema-1.1.jar"/>

<classpathentry kind="lib" path="lib/xfire/jdom.jar"/>

<classpathentry kind="lib" path="lib/xfire/commons-httpclient-3.1.jar"/>

<classpathentry kind="lib" path="lib/xfire/commons-codec-1.3.jar"/>

4.3.2测试代码
public static String invokeCallService(String xmlStr,String method) throws Exception {

Client client = null;

String res="";

try {

client = new Client(new URL(url));

Object[] result1 = client.invoke(method,new Object[] {xmlStr});

System.out.println(result1[0]);

res=(String)result1[0];

} catch (MalformedURLException e) {

e.printstacktrace();

} catch (Exception e) {

e.printstacktrace();

}

return res;

}

4.4客户端CXF
4.4.1引入jar包
<classpathentry kind="lib" path="lib/cxf/mini_and_spring/commons-logging-1.1.1.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/cxf-2.3.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/stax2-api-3.0.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/woodstox-core-asl-4.0.8.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/wsdl4j-1.6.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/neethi-2.0.4.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/XmlSchema-1.4.7.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/spring-core-2.5.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/spring-beans-2.5.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_and_spring/spring-context-2.5.2.jar"/>


4.4.2无Spring
<classpathentry kind="lib" path="lib/cxf/mini_no_spring/cxf-2.3.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_no_spring/msv-20050913.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_no_spring/stax2-api-3.0.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_no_spring/woodstox-core-asl-4.0.8.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_no_spring/wsdl4j-1.6.2.jar"/>

<classpathentry kind="lib" path="lib/cxf/mini_no_spring/XmlSchema-1.4.7.jar"/>

4.4.3测试代码
public static String invokeCallService(String xmlStr,String method) throws Exception {

JaxWsDynamicclientFactory dcf = JaxWsDynamicclientFactory.newInstance();

Client client = dcf.createClient(url);

Object[] res = null;

try {

res = client.invoke(method,xmlStr);

} catch (Exception e) {

e.printstacktrace();

}

return (String) res[0];

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐