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()); } } } } }