由于在Jdeveloper工具中不能通过wsdl生成客户端代码,所以需要使用soap协议手动构造消息以及发送消息.
/** * 构造用户消息 * @return * @throws javax.xml.soap.SOAPException */ private static SOAPMessage createUserMessage() throws SOAPException, IOException { String send = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:DefaultNamespace\">"+ "<soapenv:Header/>" + "<soapenv:Body>" + "<urn:IN_FOLDERID>*</urn:IN_FOLDERID>" + "<urn:IN_RETURNSUBDEPTUSER>1</urn:IN_RETURNSUBDEPTUSER>" + "<urn:SYSID>"+SYSID+"</urn:SYSID>" + "<urn:SYSPWD>"+SYSPWD+"</urn:SYSPWD>" + "</soapenv:Body>" + "</soapenv:Envelope>"; InputStream is = new ByteArrayInputStream(send.getBytes("UTF-8")); //getBytes("UTF-8")解决soap报文传送时的中文乱码 return MessageFactory.newInstance().createMessage(null, is); }
/** * 发送消息 * @param url * @param message * @return * @throws javax.xml.soap.SOAPException */ private static SOAPBody sendMessage(String url,SOAPMessage message) throws SOAPException, TransformerException { SOAPConnection connection = soapConnFactory.createConnection(); SOAPMessage reply = null; try{ reply = connection.call(message, url); }catch (Exception e){ e.printStackTrace(); } SOAPBody soapBody=reply.getSOAPBody(); // if(isDebug){ //输出内容在控制台 // Transformer transformer = transformerFactory.newTransformer(); // transformer.setOutputProperty("encoding","GB2312");//这句话可以设置生成后的不乱码 // Source sourceContent = reply.getSOAPPart().getContent(); // StreamResult result = new StreamResult(System.out); // transformer.transform(sourceContent, result); // System.out.println(); // } connection.close(); return soapBody; }
通过获取服务端返回来的soap报文,然后解析该xml获取相应的结果数据。
/** * 获取用户 * @param pageIndex * @return */ public List<UserBean> getUserList() throws SOAPException, TransformerException, IOException, Exception { List<UserBean> userBeanList = new ArrayList<UserBean>(); String url = BPMConstants.SERVERURL + "/WF_GetUserListSrv?wsdl"; SOAPBody soapBody = SOAPUtil.sendMessage(url); SOAPElement soapElementFirst = (SOAPElement) soapBody.getFirstChild(); String nodeNameFirst = soapElementFirst.getNodeName(); if (nodeNameFirst.equals("ns0:GETUSERReturn")) { Iterator<SOAPElement> iteratorSecond = soapElementFirst.getChildElements(); while (iteratorSecond.hasNext()) { SOAPElement soapElementSecond = iteratorSecond.next(); String nodeNameSecond = soapElementSecond.getNodeName(); if (nodeNameSecond.equals("WF_ITEM")) { UserBean userBean = new UserBean(); Iterator<SOAPElement> iteratorThree = soapElementSecond.getChildElements(); while (iteratorThree.hasNext()) { SOAPElement soapElementThree = iteratorThree.next(); String nodeNameThree = soapElementThree.getNodeName(); if (nodeNameThree.equals("WF_USERID")) { userBean.setUserId(soapElementThree.getValue()); } else if (nodeNameThree.equals("WF_USERNAME")) { userBean.setUserName(soapElementThree.getValue()); } else if (nodeNameThree.equals("WF_FOLDERID")) { userBean.setFolderId(soapElementThree.getValue()); } else if (nodeNameThree.equals("WF_DEPTNAME")) { userBean.setDeptName(soapElementThree.getValue()); } } userBeanList.add(userBean); } } } return userBeanList; }
可以通过soapUI工具查看soap协议的所需要的构造消息格式以及返回来的报文xml格式
--摘自 OA移动接口项目 OaRest