1、maven引入jar包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
<dependency><groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.1.11</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.1.11</version>
</dependency>
2、动态调用webservice接口
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.stereotype.Component;
@Component
public class WebserviceClient {
public String callWebSV(String wsdUrl, String operationName, String... params) throws Exception {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient(wsdUrl);
Object[] objects;
objects = client.invoke(operationName, params);
return objects[0].toString();
}
}
3、调用示例
@Autowired
WebserviceClient webserviceClient;
String webUrl = "http://www.webxml.com.cn/webservices/qqOnlineWebService.asmx?wsdl";
public List<Map> selectLineInfo(String qqCode) {
List<Map> list =new ArrayList<>();
try {
String methodName = "qqCheckOnline";
String xmlString = webserviceClient.callWebSV(webUrl, methodName, qqCode);
System.out.println("原始返回内容----"+xmlString);
System.out.println("获取-成功!");
return list;
} catch (Exception e) {
System.out.println("获取-失败!");
System.out.println(e);
return null;
}
}