1.application.properties文件中配置接口url
2.工具类
package com.vulnverify.core.utils;
import java.io.IOException;
import java.util.Arrays;
import java.util.Properties;
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;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.util.StringUtils;
import tns.Application;
import tns.Application_Service;
/**
*
* 使用cxf 调用webservice 接口
*
* @author linan
*
*/
public class CxfInvokeUtil {
static Logger logger = Logger.getLogger(CxfInvokeUtil.class);
private static String wsdlUrl="";
static{
String filePath = null;
try {
filePath = ApplicationUtils
.getWebFileAbsoluteClassPath("application.properties");
} catch (IOException e) {
e.printStackTrace();
}
Properties pro = FileUtil.loadProperties(filePath);
wsdlUrl = pro.getProperty("cxf.url");
logger.info("初始化cxf调用webservice地址为:"+wsdlUrl);
}
/**
*
* 调用webservice 接口
*
* @param method
* 调用方法名
*
* @param params
* 接口传入参数
*
* @return
*
*/
public static synchronized Object[] invoke(String method,Object params) {
Object[] objects = null;
String paramsStr = null;
try {
ObjectMapper om = new ObjectMapper();
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory
.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient(wsdlUrl);
//如果返回的address不是远程服务地址,重新制定地址
client.getEndpoint().getEndpointInfo().setAddress(wsdlUrl);
if (StringUtils.isEmpty(method)) {
logger.error("cxf 调用webservice 执行方法名缺失:method 未传入");
return objects;
}
paramsStr = om.writeValueAsString(params);
logger.info("cxf 调用webservice method:"+method+",参数:" + paramsStr);
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy(); // 策略
httpClientPolicy.setConnectionTimeout( 36000 ); //连接超时
httpClientPolicy.setAllowChunking( false );
httpClientPolicy.setReceiveTimeout( 10000 ); //接收超时
HTTPConduit http = (HTTPConduit) client.getConduit();
http.setClient(httpClientPolicy);
objects = client.invoke(method, paramsStr);
} catch (Exception e) {
e.printStackTrace();
logger.error("cxf 调用webservice 执行错误:", e);
}
return objects;
}
public static void main(String[] args) throws Exception {
Object[] invoke = CxfInvokeUtil.invoke("get_images", "");
System.out.println(invoke);
}
}