分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

cxf 生成 webservice 客户端

发布时间:2023-09-06 02:03责任编辑:蔡小小关键词: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 客户端

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

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved