最近項目中有朋友諮詢我webService接口的調用,在這裏總結並分享一下,我對webService方法的調用。
一、webService接口調用工具類
public class WebService {
private WebService() {
throw new IllegalAccessError("CallWebServiceInterface class");
}
public static <E> E getWebServiceClient(String url, Class<E> clazz, int connectionTimeout, int receiveTimeout) {
JaxWsProxyFactoryBean proxy = new JaxWsProxyFactoryBean();
proxy.setServiceClass(clazz);
proxy.setAddress(url);
@SuppressWarnings("unchecked")
E client = (E) proxy.create();
// 配置超時時間
Client clientProxy = ClientProxy.getClient(client);
HTTPConduit conduit = (HTTPConduit) clientProxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(connectionTimeout); // 連接超時時間
policy.setReceiveTimeout(receiveTimeout);// 請求超時時間
conduit.setClient(policy);
return client;
}
public static <E> E getWebServiceClient(String url, Class<E> clazz) {
return getWebServiceClient(url, clazz, 5000, 5000);
}
}
二、webService服務接口
@WebService(targetNamespace = "http://biz.cos.interfaces.ccs.sf.com/")
public interface webServiceTest{
@WebMethod
List<String> queryAddressByPhone(String args);
}
注意:
1、webService註解上的targetNamespace是接口提供方的分包地址,獲取方式可以直接訪問webService接口地址返回的XML文件中獲取;
2、接口中的方法名必須帶webMethod註解,並且接口方法必須與提供方的接口一模一樣;
參數詳解:
1、url爲接口供應商提供的webService接口地址
2、args爲webService方法需求的參數
public List<String> queryAddressByPhone(String url , String args) {
webServiceTest client = WebService.getWebServiceClient(url, webServiceTest.class);
List<String> Result = client.queryAddressByPhone(args);
return Result;
}