按下面步骤逐步搭建cxf客户端代码:
1、下载cxf
2、在bin目录下生成客户端存根:wsdl2java -encoding utf-8 http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl
3、主要是使用上面存根的接口类,单做本地代码来使用,
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IpAddressSearchWebServiceSoap.class);//设置请求接口
factory.setAddress("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl");
IpAddressSearchWebServiceSoap esbServer = (IpAddressSearchWebServiceSoap) factory.create(); //创建客户端对象
//设置客户端的配置信息,超时等.
Client proxy = ClientProxy.getClient(esbServer);
HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
HTTPClientPolicy policy = new HTTPClientPolicy();
policy.setConnectionTimeout(10000); //连接超时时间
policy.setReceiveTimeout(12000);//请求超时时间.
conduit.setClient(policy);
System.out.println("测试:" + esbServer.getCountryCityByIp("119.23.204.XX").getString()); //调用方法。
4、在springboot中引入cxf相关类;
<!-- CXF webservice -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-spring-boot-starter-jaxws</artifactId>
<version>3.1.11</version>
</dependency>
补充:
cxf默认30秒超时
用命令生成的存根,其实
另外一种写法如下,不过没弄清楚超时时间加在哪里合适
// System.out.println(new IpAddressSearchWebService().getIpAddressSearchWebServiceSoap().getCountryCityByIp("119.23.204.XX").getString());
// System.out.println("2222222222222222222222222222");