Webservice初体验:axis调用wsdl和document解析
Neo君遇到了需要调用webservice接口的情况,然而作为一只小白对这方面了解困乏,经过摸索,完成了一个小的Demo,记录下。
一:背景
同事给了我一个webservice地址,打开后后缀是"...?wsdl",将链接"http://......?wsdl"放入soapUI中,可以解析出完整的。但是Neo君之前对这边是空白的,上网搜索了一些webservice和soap以及wsdl的基本知识后,才了解了一些。但是具体到代码里使用,网上搜到的内容好多都是copy的,且不完整。
二:Code
话不多说,中间走过的弯路一并带过,直接贴代码。
1 import org.apache.axis.client.Call; 2 import org.apache.axis.client.Service; 3 import org.apache.axis.encoding.XMLType; 4 import org.apache.axis.message.MessageElement; 5 import org.apache.axis.message.SOAPHeaderElement; 6 import org.apache.axis.types.Schema; 7 import org.dom4j.Document; 8 import org.dom4j.io.SAXReader; 9 10 import javax.xml.namespace.QName; 11 import javax.xml.soap.SOAPElement; 12 import javax.xml.soap.SOAPFactory; 13 import java.io.ByteArrayInputStream; 14 15 16 public class WebserviceUtilTest { 17 public static void main(String[] args) { 18 try{ 19 String endpoint="http://xxx"; 20 // 直接引用远程的wsdl文件,"?wsdl"前的链接 21 String method="test"; 22 String namespace = "http://www.xxxx"; 23 24 Service service = new Service(); 25 Call call = (Call) service.createCall(); 26 //设置服务器地址 27 call.setTargetEndpointAddress(new java.net.URL(endpoint)); 28 //设置命名空间和调用方法 29 call.setOperationName(new QName(namespace,method));// WSDL里面描述的接口名称 30 call.setUseSOAPAction(true); 31 32 33 //设置header,如果有security需求的话 34 SOAPHeaderElement soapHeaderElement = createHeader(); 35 if(soapHeaderElement == null){ 36 System.out.println("====Exception"); 37 return; 38 } 39 call.addHeader(soapHeaderElement); 40 41 //添加方法的参数,有几个添加几个 42 call.addParameter("param1", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);// 接口的参数 43 call.addParameter("param2", XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);// 接口的参数 44 //设置返回类型 45 46 /*返回类型可以不设置,如果是简单类型可以使用XMLType.XSD_STRING等简单类型,这里使用schema解析返回的报文。 47 如果接收的类型和设置的类型不一致,会报以下异常: 48 SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.*/ 49 call.setReturnType(XMLType.XSD_SCHEMA);// 设置返回类型 50 51 52 53 String param1Value = "test"; 54 String param2Value = "test"; 55 Schema result = (Schema) call.invoke(new Object[] { account,passwd }); 56 57 //解析返回结果 58 MessageElement[] data = result.get_any(); 59 for(int i=0; i<data.length; i++){ 60 SAXReader reader = new SAXReader(); 61 Document doc = reader.read(new ByteArrayInputStream(data[i].toString().getBytes())); 62 System.out.println(doc.getStringValue()); 63 } 64 65 66 //输出SOAP请求报文 67 System.out.println("--SOAP Request: " + call.getMessageContext().getRequestMessage().getSOAPPartAsString()); 68 //输出SOAP返回报文 69 System.out.println("--SOAP Response: " + call.getResponseMessage().getSOAPPartAsString()); 70 71 72 }catch (Exception e){ 73 e.printStackTrace(); 74 75 } 76 } 77 /*构造Header*/ 78 public static SOAPHeaderElement createHeader(){ 79 String AUTH_PREFIX = "wsse"; 80 String AUTH_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; 81 String TYPE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"; 82 83 String userName= "admin"; 84 String passwd= "admin"; 85 86 87 //SOAPHeaderElement soapHeaderElement =new SOAPHeaderElement(); 88 89 90 try{ 91 SOAPFactory soapFactory =SOAPFactory.newInstance(); 92 SOAPElement wsSecHeaderElm = soapFactory.createElement("Security", AUTH_PREFIX, AUTH_NS); 93 SOAPElement userNameTokenElm = soapFactory.createElement("UsernameToken",AUTH_PREFIX, AUTH_NS); 94 SOAPElement userNameElm = soapFactory.createElement("Username",AUTH_PREFIX, AUTH_NS); 95 SOAPElement passwdElm = soapFactory.createElement("Password",AUTH_PREFIX, AUTH_NS); 96 passwdElm.setAttribute("Type", TYPE); 97 98 userNameElm.addTextNode(userName); 99 passwdElm.addTextNode(passwd); 100 101 userNameTokenElm.addChildElement(userNameElm); 102 userNameTokenElm.addChildElement(passwdElm); 103 wsSecHeaderElm.addChildElement(userNameTokenElm); 104 105 SOAPHeaderElement soapHeaderElement = new SOAPHeaderElement(wsSecHeaderElm); 106 soapHeaderElement.setMustUnderstand(true); 107 108 return soapHeaderElement; 109 }catch(Exception e) { 110 e.printStackTrace(); 111 return null; 112 } 113 } 114 }