1.application.properties文件中配置接口urlhtml
2.工具類java
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); } }