news 2026/8/27 19:03:22

java-工具-Webservice wsdl解析

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
java-工具-Webservice wsdl解析

原文转载:https://www.cnblogs.com/coshaho/p/5689738.html

public class WsdlInfo{private String wsdlName;private List<InterfaceInfo>interfaces;/** * coshaho * @param path wsdl地址 * @throws Exception */ public WsdlInfo(String path)throws Exception{WsdlProject project=new WsdlProject();WsdlInterface[]wsdlInterfaces=WsdlImporter.importWsdl(project, path);this.wsdlName=path;if(null!=wsdlInterfaces){List<InterfaceInfo>interfaces=new ArrayList<InterfaceInfo>();for(WsdlInterface wsdlInterface:wsdlInterfaces){InterfaceInfo interfaceInfo=new InterfaceInfo(wsdlInterface);interfaces.add(interfaceInfo);}this.interfaces=interfaces;}}public StringgetWsdlName(){returnwsdlName;}public void setWsdlName(String wsdlName){this.wsdlName=wsdlName;}public List<InterfaceInfo>getInterfaces(){returninterfaces;}public void setInterfaces(List<InterfaceInfo>interfaces){this.interfaces=interfaces;}}public class InterfaceInfo{private String interfaceName;private List<OperationInfo>operations;private String[]adrress;public InterfaceInfo(WsdlInterface wsdlInterface){this.interfaceName=wsdlInterface.getName();this.adrress=wsdlInterface.getEndpoints();int operationNum=wsdlInterface.getOperationCount();List<OperationInfo>operations=new ArrayList<OperationInfo>();for(int i=0;i<operationNum;i++){WsdlOperation operation=(WsdlOperation)wsdlInterface.getOperationAt(i);OperationInfo operationInfo=new OperationInfo(operation);operations.add(operationInfo);}this.operations=operations;}public StringgetInterfaceName(){returninterfaceName;}public void setInterfaceName(String interfaceName){this.interfaceName=interfaceName;}public List<OperationInfo>getOperations(){returnoperations;}public void setOperations(List<OperationInfo>operations){this.operations=operations;}public String[]getAdrress(){returnadrress;}public void setAdrress(String[]adrress){this.adrress=adrress;}}public class OperationInfo{private String operationName;private String requestXml;private String responseXml;public OperationInfo(WsdlOperation operation){operationName=operation.getName();requestXml=operation.createRequest(true);responseXml=operation.createResponse(true);}public StringgetOperationName(){returnoperationName;}public void setOperationName(String operationName){this.operationName=operationName;}public StringgetRequestXml(){returnrequestXml;}public void setRequestXml(String requestXml){this.requestXml=requestXml;}public StringgetResponseXml(){returnresponseXml;}public void setResponseXml(String responseXml){this.responseXml=responseXml;}}public class WSDLParseTest{public static void main(String[]args)throws Exception{String url="http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx?wsdl";WsdlInfo wsdlInfo=new WsdlInfo(url);System.out.println("WSDL URL is "+ wsdlInfo.getWsdlName());for(InterfaceInfo interfaceInfo:wsdlInfo.getInterfaces()){System.out.println("Interface name is "+ interfaceInfo.getInterfaceName());for(String ads:interfaceInfo.getAdrress()){System.out.println("Interface address is "+ ads);}for(OperationInfo operation:interfaceInfo.getOperations()){System.out.println("operation name is "+ operation.getOperationName());System.out.println("operation request is ");System.out.println("operation request is "+ operation.getRequestXml());System.out.println("operation response is ");System.out.println(operation.getResponseXml());}}}}

第一中httpURLConnection方式调用

package com.ssh.webserviceTSY; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; public class Test { public static void main(String[] args) throws IOException { //第一步:创建服务地址 URL url = new URL("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl"); //第二步:打开一个通向服务地址的连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //第三步:设置参数 //3.1发送方式设置:POST必须大写 connection.setRequestMethod("POST"); //3.2设置数据格式:content-type connection.setRequestProperty("content-type", "text/xml;charset=utf-8"); //3.3设置输入输出,因为默认新创建的connection没有读写权限, connection.setDoInput(true); connection.setDoOutput(true); //第四步:组织SOAP数据,发送请求 String soapXML = getXML("17321242779"); //将信息以流的方式发送出去 OutputStream os = connection.getOutputStream(); os.write(soapXML.getBytes()); //第五步:接收服务端响应,打印 int responseCode = connection.getResponseCode(); if(200 == responseCode){//表示服务端响应成功 //获取当前连接请求返回的数据流 InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String temp = null; while(null != (temp = br.readLine())){ sb.append(temp); } /** * 打印结果 */ System.out.println(sb.toString()); is.close(); isr.close(); br.close(); } os.close(); } public static String getXML(String phone){ String soapXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<soap:Envelope xmlns:xsi=\"http://www.w3.org/2003/XMLSchema-instance\" " +"xmlns:web=\"http://WebXml.com.cn/\" " +"xmlns:xsd=\"http://www.w3.org/2003/XMLSchema\" " +"xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">" +"<soap:Body>" +"<web:getMobileCodeInfo>" +phone +"</web:getMobileCodeInfo>" +"</soap:Body>" +"</soap:Envelope>"; return soapXML; } }

第二中httpURLConnection方式调用

package com.zyh.car.util; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import java.util.List; import java.util.Map; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import javax.annotation.Resource; public class GetEquipMsgUtils { public static void main(String[] args) throws IOException { //第一步:创建服务地址,也就是提供的WSDL的接口地址 URL url = new URL("http://xxxxxxxxxxxxxxxx/xxxx/xxxxxxxxx?wsdl"); //第二步:打开一个通向服务地址的连接 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //第三步:设置参数 //3.1发送方式设置:POST必须大写 connection.setRequestMethod("POST"); //3.2设置数据格式:content-type connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); //3.3设置输入输出,因为默认新创建的connection没有读写权限, connection.setDoInput(true); connection.setDoOutput(true); //第四步:组织SOAP数据,发送请求, String soapXML = getXML(方法名,参数1,参数2); //将信息以流的方式发送出去 OutputStream os = connection.getOutputStream(); os.write(soapXML.getBytes()); //第五步:接收服务端响应,打印 int responseCode = connection.getResponseCode(); if(200 == responseCode){//表示服务端响应成功 //获取当前连接请求返回的数据流 InputStream is = connection.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); String temp = null; while(null != (temp = br.readLine())){ sb.append(temp); } 或者 int BUFFER_SIZE = 1024; char[] buffer = new char[BUFFER_SIZE]; // or some other size, int charsRead = 0; while ( (charsRead = br.read(buffer, 0, BUFFER_SIZE)) != -1) { sb.append(buffer, 0, charsRead); } /** * 打印结果 */ System.out.println("---"+sb.toString()); try{ //对返回的数据进行JSON格式化 System.out.println(xml2Json(sb.toString())); }catch (Exception e){ e.printStackTrace(); } is.close(); isr.close(); br.close(); } os.close(); } //请求入参 public static String getXML(String method,int begin,int end){ String soapXML = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tns=\"http://guizhou.fire.and.rescue\">" +"<soapenv:Header></soapenv:Header> " +"<soapenv:Body> " +"<tns:"+method+"> "//方法名 +"<tns:offset>"+begin+"</tns:offset>"//参数1 +"<tns:row_count>"+end+"</tns:row_count>"//参数2 +"</tns:"+method+">" +"</soapenv:Body>" +"</soapenv:Envelope>"; return soapXML; } /** * xml转json * * @param xmlStr * @return * @throws DocumentException */ public static JSONObject xml2Json(String xmlStr) throws DocumentException { Document doc = DocumentHelper.parseText(xmlStr); JSONObject json = new JSONObject(); dom4j2Json(doc.getRootElement(), json); return json; } /** * xml转json * * @param element * @param json */ public static void dom4j2Json(Element element, JSONObject json) { List<Element> chdEl = element.elements(); for(Element e : chdEl){ if (!e.elements().isEmpty()) { JSONObject chdjson = new JSONObject(); dom4j2Json(e, chdjson); Object o = json.get(e.getName()); if (o != null) { JSONArray jsona = null; if (o instanceof JSONObject) { JSONObject jsono = (JSONObject) o; json.remove(e.getName()); jsona = new JSONArray(); jsona.add(jsono); jsona.add(chdjson); } if (o instanceof JSONArray) { jsona = (JSONArray) o; jsona.add(chdjson); } json.put(e.getName(), jsona); } else { if (!chdjson.isEmpty()) { json.put(e.getName(), chdjson); } } } else { if (!e.getText().isEmpty()) { json.put(e.getName(), e.getText()); } } } } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/27 19:02:57

虚拟电厂总体规划建设方案【附全文阅读】

这份 32 页虚拟电厂总体规划 PPT 是新型电力系统、综合能源、负荷聚合类项目投标、可研编制、业务宣讲核心标准化推介素材&#xff0c;适配电网、售电公司、园区能源服务商各类场景。文档紧扣新能源消纳、电力市场化改革政策&#xff0c;精准剖析风光间歇性、电网峰谷差大、分布…

作者头像 李华
网站建设 2026/8/27 18:59:59

0 基础大学生如何入局网络安全?学习路线、避坑、就业全梳理

0 基础入局网络安全&#xff1a;大学生逆袭高薪的秘密武器&#xff01; 宝子们&#xff01;最近我的后台简直要被大学生们的私信“淹没”啦&#xff0c;全是关于网络安全转行的问题。看来大家对未来的职业规划都挺上心的&#xff0c;我特别欣慰&#xff01;今天咱就敞开了好好…

作者头像 李华
网站建设 2026/8/27 18:54:58

阿里、腾讯、美团春招真题“惨遭”泄露,Github上标星66.3K

程序员春招 黄金跳槽的高峰期已经到来&#xff0c;今年市场变得格外不同&#xff0c;比之前仅仅想涨薪、想换领导的基础因素上又加了两种情况&#xff1b;如受前两年疫情影响想跳槽未跳的&#xff0c;去年“双减”政策裁员的…所以导致今年的市场更加火热&#xff1b; 众所周知…

作者头像 李华
网站建设 2026/8/27 18:44:36

dm8临时表空间使用率查询-达梦数据库

DM8临时表空间使用率查询-达梦数据库1. 概述2. 创建测试表3. 插入测试数据以触发排序4. 执行触发临时表空间的查询5. 验证临时表空间使用情况6. 注意事项与优化建议7. 总结8. 更多达梦数据库全方位指南&#xff1a;安装、优化与实战教程-1. 概述 在达梦数据库&#xff08;DM D…

作者头像 李华