cxf 生成 webservice 客户端

Posted 方方方方方方

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cxf 生成 webservice 客户端相关的知识,希望对你有一定的参考价值。

一、cxf 生成 webservice 客户端

1、接口路径 http://localhost:8080/cxfserver/webservice/userWS?wsdl 

2、进入你需要放置 webservice 客户端代码的包,进入这个包所在的系统路径,进入 cmd

3、执行命令 wsimport -keep http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 或者 wsdl2java -client http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 

  3.1、如果报错信息如下:具有相同名称“xxx”的类/接口已经使用。

  wsdl2java -client http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 

  改为 

  wsdl2java -client -autoNameResolution http://ws.webxml.com.cn/WebServices/WeatherWS.asmx?wsdl 

4、spring 整合 cxf

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
    <jaxws:client id="userClient"
        serviceClass="com.java.webservice.service.impl.ITianQi"   <!--生成的接口-->
        address="http://ws.webxml.com.cn/WebServices/WeatherWS.asmx">       
    </jaxws:client>
</beans> 

二、httpclient 调用 webservice

public static String getXML() {
    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version=‘1.0‘ encoding=‘utf-8‘?>"
            + "<soap:Envelope xmlns:xsi=‘http://www.w3.org/2001/XMLSchema-instance‘ xmlns:xsd=‘http://www.w3.org/2001/XMLSchema‘ xmlns:soap=‘http://schemas.xmlsoap.org/soap/envelope/‘>"
            + "<soap:Body>" + "<getMobileCodeInfo xmlns=‘http://WebXml.com.cn/‘>"
            + "<mobileCode>string</mobileCode>" + " <userID>string</userID>" + "</getMobileCodeInfo>"
            + "</soap:Body>" + "</soap:Envelope>");
    return sb.toString();
}
static int socketTimeout = 30000;// 请求超时时间 static int connectTimeout = 30000;// 传输超时时间 public static String doPost() { String postUrl = "http://ws.webxml.com.cn/WebServices/WeatherWS.asmx"; String soapAction = ""; String soapXml = getXML(); String retStr = ""; // 创建HttpClientBuilder HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); // HttpClient CloseableHttpClient closeableHttpClient = httpClientBuilder.build(); HttpPost httpPost = new HttpPost(postUrl); // 设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout) .setConnectTimeout(connectTimeout).build(); httpPost.setConfig(requestConfig); try { httpPost.setHeader("Content-Type", "application/soap+xml;charset=UTF-8"); httpPost.setHeader("SOAPAction", soapAction); StringEntity data = new StringEntity(soapXml, Charset.forName("UTF-8")); httpPost.setEntity(data); CloseableHttpResponse response = closeableHttpClient.execute(httpPost); HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) { // 打印响应内容 retStr = EntityUtils.toString(httpEntity, "UTF-8"); } // 释放资源 closeableHttpClient.close(); } catch (Exception e) { } return retStr; }

 


以上是关于cxf 生成 webservice 客户端的主要内容,如果未能解决你的问题,请参考以下文章

cxf webservice生成客户端代码及调用服务端遇到的问题

cxf如何创建webservice客户端

使用CXF开发WebService客户端

使用CXF开发WebService客户端

通过JDK自带工具生成WebService客户端代码

学习webservice之cxf:使用webservice实现客户端