大家好,又見面了,我是你們的朋友全棧君。
WebServiceUtils 工具類
package xxxx;
import java.util.ResourceBundle;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.frontend.ClientProxy;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.springframework.stereotype.Service;
@Service
public class WebServiceUtils {
public static final int CXF_CLIENT_CONNECT_TIMEOUT = 30 * 1000;
public static final int CXF_CLIENT_RECEIVE_TIMEOUT = 30 * 1000;
/**
*
* @param clazz
* @param paraName
* @param timeout
* @return
*/
public <T> T getWebService(Class<T> clazz, String paraName, Integer... timeout) {
ResourceBundle dBResources = ResourceBundle.getBundle("application-config");
String url = dBResources.getString(paraName);
if (timeout == null || timeout.length == 0) {
return getWebServiceByUrl(clazz, url);
} else if (timeout.length == 1) {
return getWebServiceByUrl(clazz, url, timeout[0], timeout[0]);
} else {
return getWebServiceByUrl(clazz, url, timeout[0], timeout[1]);
}
}
public <T> T getWebServiceByUrl(Class<T> clazz, String url) {
return getWebServiceByUrl(clazz, url, CXF_CLIENT_CONNECT_TIMEOUT, CXF_CLIENT_RECEIVE_TIMEOUT);
}
private <T> T getWebServiceByUrl(Class<T> clazz, String url, Integer connetTimeout, Integer receiveTimeout) {
// 用于建立JAX-WS代理的工廠,此類提供對用于設定代理的内部屬性的通路。使用它可以提供比标準JAX-WS API更多的控制。
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(clazz);
factory.setAddress(url);
@SuppressWarnings("unchecked")
// 建立可用于進行遠端調用的代理對象。
T webService = (T) factory.create();
// 設定接口 連接配接逾時和請求逾時
// 通過代理對象擷取本地用戶端
Client proxy = ClientProxy.getClient(webService);
// 通過本地用戶端設定 網絡政策配置
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
// 用于配置用戶端HTTP端口的屬性
HTTPClientPolicy policy = new HTTPClientPolicy();
// 逾時控制 機關 : 毫秒
policy.setConnectionTimeout(connetTimeout);
policy.setReceiveTimeout(receiveTimeout);
conduit.setClient(policy);
return webService;
}
}
複制
實際調用示例
String jsonoInfo = null;
try {
jsonInfo = webServiceUtil.getWebService(GjxxWebService.class,
"webService_url").gjglAccountqueryPersonFuzzyWs(zjhm, idType, pageNo, pageSize);
} catch (WebServiceException se) {
xxxxxxx....
}
複制
各參數名稱等分析:
名稱 | 描述 |
---|---|
GjxxWebService | 通過wsdl 生成的接口 |
webService_url | 接口的url位址 |
gjglAccountqueryPersonFuzzyWs | 接口中的某個方法 |
zjhm, idType, pageNo, pageSize | 調用該接口所需要的參數 |
jsonInfo | 調用該接口的傳回值,據接口具體而定 |
釋出者:全棧程式員棧長,轉載請注明出處:https://javaforall.cn/162672.html原文連結:https://javaforall.cn