码迷,mamicode.com
首页 > Web开发 > 详细

cxf 生成 webservice 客户端

时间:2018-07-12 20:07:11      阅读:34      评论:0      收藏:0      [点我收藏+]

标签:生成   version   catch   ons   资源   3.1   set   The   asmx   

一、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 客户端

标签:生成   version   catch   ons   资源   3.1   set   The   asmx   

原文地址:https://www.cnblogs.com/fangwu/p/9300895.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!