使用Apache CXF框架开发webservice服务的客户端

场景: webservice服务的客户端用来访问webservice这是个异构的架构,
     本客户端使用Java开发,服务端不限编程语言,只需遵循wsdl规范即可。
1.在开发客户端时,会得到需要访问的服务的发布服务地址,本例使用如下地址:
  http://127.0.0.1:8080/study/ws/publicService?wsdl  
  本例的服务端可参考: 服务端实现
2.在浏览器中访问 http://127.0.0.1:8080/study/ws/publicService?wsdl 
  可以得到wsdl文档信息,说明对方服务正常且可用,开发客户端需要从中获取相关信息
  <1>.服务方发布了什么方法,方法的入参类型,方法返回类型,入参个数,从文档可以找到:
      本例方法是:getCityInfo
      入参类型: string 
      <xs:element minOccurs="0" name="arg0" type="xs:string"/>  
      返回类型: string 
      <xs:element minOccurs="0" name="return" type="xs:string"/>
  <2>.服务的名称空间
      http://webservice.zbz.com/
3.代码如下: 

package com.zbz.client;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.xml.namespace.QName;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
/**
 *  CxfClient可以访问服务端发布的webservice服务
 * @ClassName: CxfClient
 * @date: 2018-12-30 下午12:53:32
 */
public class CxfClient {
	/**发布服务方发布的地址*/
	final static String wsdlUrl = "http://127.0.0.1:8080/study/ws/publicService?wsdl";
	final static String targetNamespace = "http://webservice.zbz.com/";
	final static long receiveTimeout = 1000 * 90;    // 90秒 cxf 默认是60秒
	final static long connectionTimeout = 1000 * 30; // 30秒  
	/**本客户端调用CXF服务*/
	public static void invokeCxf(String method, Object... args) {
		JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
		QName service = new QName(targetNamespace, "PublicInterfaceService");
		QName namePort = new QName(targetNamespace, "PublicInterfacePort");
		Client client = factory.createClient(wsdlUrl, service, namePort);
		// 获取HttpClient代理,并设置代理的端口号,超时时间等。
		HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
		// 设置超时
		HTTPConduit confuit = (HTTPConduit) client.getConduit();
		// 设置连接超时
		httpClientPolicy.setConnectionTimeout(connectionTimeout);
		// 设置读取超时
		httpClientPolicy.setReceiveTimeout(receiveTimeout);
		confuit.setClient(httpClientPolicy);
		Object[] result;
		System.out.println(getCurrentTime() +" : "+ "CXF客户端开始调用服务.....");
		try {
			result = client.invoke(method, args);
			System.out.println(result[0]);
			System.out.println(getCurrentTime() +" : "+ "CXF客户端结束调用服务.....");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String []args){
		
		System.out.println(getCurrentTime() +" : 开始测试....");
		String method = "getCityInfo";
		String arg0 ="厦门";
		invokeCxf(method,arg0);
		System.out.println(getCurrentTime() +" : 结束测试....");
	}
	/**提供格式化时间2018-10-10*/
	public static String getCurrentTime() {
		Date now = new Date();
		SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		System.currentTimeMillis();
		return ft.format(now);
	}
}


解析: <1>.invokeCxf(String method, Object... args)简单封装,把CXF相关调用封装在这里
      <2>.代码解析
      QName service = new QName(targetNamespace, "PublicInterfaceService");
      QName namePort = new QName(targetNamespace, "PublicInterfacePort");
      这两行代码中:PublicInterfaceService和PublicInterfacePort来wsdl文档中
      <wsdl:service name="PublicInterfaceService">
      <wsdl:port binding="tns:PublicInterfaceServiceSoapBinding" name="PublicInterfacePort">
      <soap:address location="http://127.0.0.1:8080/study/ws/publicService"/>
      </wsdl:port>
      </wsdl:service>
      <3>.targetNamespace定义的一个字符串属性,值是来自wsdl文档中的名称空间
       "http://webservice.zbz.com/"

4.如下完整的wsdl文档信息

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webservice.zbz.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="PublicInterfaceService" targetNamespace="http://webservice.zbz.com/">
<wsdl:types>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.zbz.com/" elementFormDefault="unqualified" targetNamespace="http://webservice.zbz.com/" version="1.0">
<xs:element name="getCityInfo" type="tns:getCityInfo"/>
<xs:element name="getCityInfoResponse" type="tns:getCityInfoResponse"/>
<xs:complexType name="getCityInfo">
<xs:sequence>
<xs:element minOccurs="0" name="arg0" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="getCityInfoResponse">
<xs:sequence>
<xs:element minOccurs="0" name="return" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
<wsdl:message name="getCityInfoResponse">
<wsdl:part element="tns:getCityInfoResponse" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:message name="getCityInfo">
<wsdl:part element="tns:getCityInfo" name="parameters"></wsdl:part>
</wsdl:message>
<wsdl:portType name="PublicInterface">
<wsdl:operation name="getCityInfo">
<wsdl:input message="tns:getCityInfo" name="getCityInfo"></wsdl:input>
<wsdl:output message="tns:getCityInfoResponse" name="getCityInfoResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="PublicInterfaceServiceSoapBinding" type="tns:PublicInterface">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="getCityInfo">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="getCityInfo">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="getCityInfoResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="PublicInterfaceService">
<wsdl:port binding="tns:PublicInterfaceServiceSoapBinding" name="PublicInterfacePort">
<soap:address location="http://127.0.0.1:8080/study/ws/publicService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


以上,TKS.

猜你喜欢

转载自blog.csdn.net/zhangbeizhen18/article/details/85383121