当前位置: 首页 > news >正文

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()); } } } } }
http://www.cnnetsun.cn/news/4251248.html

相关文章:

  • 虚拟电厂总体规划建设方案【附全文阅读】
  • 0 基础大学生如何入局网络安全?学习路线、避坑、就业全梳理
  • 阿里、腾讯、美团春招真题“惨遭”泄露,Github上标星66.3K
  • 告别复制粘贴式降级:纳米AI鸿蒙版导出word格式为何绕不开“AI 导出鸭”
  • 【项目编号:project19227】Spring Boot 宠物寄养平台实战:预约、健康监测与寄养人员协同
  • dm8临时表空间使用率查询-达梦数据库
  • MySQL DQL 数据查询
  • 2026年度国自然申报全流程要点梳理与避错指南
  • 大模型算法岗常见面试题100道(值得收藏)
  • 具身智能投资热潮:聪明钱究竟在争夺什么?
  • 国君产业研究汽车报告|大模型赋能座舱,智能座舱新战场(附PDF)
  • 大模型API开发中的thought traces:可解释性、调试与工程实践
  • 【行业】AI大爆发时代,巨头下场!互联网+医疗服务模式正加速创新!
  • 容器变慢先查限流和请求排队
  • 王兴兴与梁文锋“错配”背后:具身智能与大模型的真实差距
  • Wine Ubuntu 调用 Windows 应用
  • 2024请收好这一份全面且详细的AI产品经理从业指南,错过会后悔!!
  • 资深开发 / 架构师|3 个月可执行学习实践计划表
  • 中年中产程序员春节低成本自驾游:从西安出发到深圳阳江海陵岛,海南岛10天深度度假游(1)-- 海陵岛
  • 【AI大模型】写给小白的大模型应用科普:RAG篇
  • 存量系统迁移,用小变更保持主干可用
  • RustDesk部署到linux(自建服务器)
  • python的图论工业场景模拟第三篇:电力网架连通分量与孤岛区域诊断,任务解析电力网络图,找出因开关跳闸形成的孤立供电区(连通分量),图建模说明,无向图,节点=配电节点,边=闭合的开关。
  • 设计开发协作的令牌化落地方法
  • 万字长文!一文了解归一化:从Transformer归一化到主流大模型归一化的演变!
  • AutoClicker
  • 小白学大模型:提示词工程(Prompt)与Agent,简单易懂,轻松拿捏!!
  • 2026年亲测最值得推荐的5款降AI率工具
  • 2026年云台摄像头口碑榜 高好评家用产品特点与选购参考
  • Grok刷屏解析:从聊天模型到AI执行体的工程接入指南