浏览 3522 次
锁定老帖子 主题:学习使用JAXM处理SOAP
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-07-18
刚刚看完JAXM的教程,赶紧做个学习记录,以备以后的温习。 我使用的是Myeclipse开发工具,包含了大部分jar包了,还差一个JAXM-API包需要另外加进来,我是在我安装的JEE5的文件夹里找到的。(在附件里供下载) 使用JAXM发送SOAP有两种方法:SOAPConnection和Messaging providers,而编码SOAP的方法却是一样的,具体的还是在代码里体现吧。 这段代码使用的是SOAPConnection: public static void main(String[] args) throws UnsupportedOperationException, SOAPException { // Create SOAPConnection, two steps: SOAPConnectionFactory soapFactory = SOAPConnectionFactory.newInstance(); SOAPConnection con = soapFactory.createConnection(); // Create SOAPMessage MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage message = messageFactory.createMessage(); // Create SOAPPart SOAPPart soapPart = message.getSOAPPart(); // Create SOAPEnvelope SOAPEnvelope soapEnvelope = soapPart.getEnvelope(); // Create SOAPHeader SOAPHeader soapHeader = soapEnvelope.getHeader(); soapHeader.detachNode(); //delete Header node // Create SOAPBody SOAPBody soapBody = soapEnvelope.getBody(); Name bodyName = soapEnvelope.createName("getLastTradePrice", "m", "http://http://smartzxy.iteye.com"); SOAPBodyElement gltp = soapBody.addBodyElement(bodyName); // insert content to <getLastTradePrice/> Name name=soapEnvelope.createName("Symbol"); SOAPElement symbol=soapBody.addChildElement(name); symbol.addTextNode("SUNW"); try { //Print the packed SOAP message message.writeTo(System.out); } catch (IOException e) { e.printStackTrace(); } //Send SOAP message URLEndpoint endpoint=new URLEndpoint("http://smartzxy.iteye.com"); SOAPMessage response=con.call(message, endpoint); con.close(); }
这段代码使用的是Messaging Provider: public static void main(String[] args) throws NamingException, SOAPException, ParserConfigurationException, MalformedURLException { // Create Messaging Provider to send SOAP message Context ctx = new InitialContext(); ProviderConnectionFactory pcFactory = (ProviderConnectionFactory) ctx .lookup("Test1"); ProviderConnection pcCon = pcFactory.createConnection(); // Get a Message Factory ProviderMetaData metaData = pcCon.getMetaData(); String[] supportedProfiles = metaData.getSupportedProfiles(); String profile = null; for (int i = 0; i < supportedProfiles.length; i++) { if (supportedProfiles[i].equals("ebxml")) { profile = supportedProfiles[i]; break; } } MessageFactory factory = pcCon.createMessageFactory(profile); // Create SOAP message EbXMLMessageImpl message=(EbXMLMessageImpl)factory.createMessage(); SOAPMessage message = factory.createMessage(); SOAPPart soappart = message.getSOAPPart(); // Create SOAP message from existed xml file DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); try { Document doc = dBuilder.parse("e:\\soap.xml"); DOMSource domSource = new DOMSource(doc); soappart.setContent(domSource); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // Add attachment String str = "Update address for Sunny Skies " + "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439"; AttachmentPart attachment = message.createAttachmentPart(); attachment.setContent(str, "text/plain"); attachment.setContentId("address"); message.addAttachmentPart(attachment); //Add jpeg attachment AttachmentPart attachment2=message.createAttachmentPart(); byte[] jpegData=…; ByteArrayInputString stream=new ByteArrayInputStream(jpegData); attachment2.setContent(stream,”image/jpeg”); message.addAttachmentPart(attachment); //Another way to add jpeg attachment URL url = new URL("http://greatproducts.com/gizmos/img.jpg"); DataHandler dh = new DataHandler(url); AttachmentPart attachment3 = message.createAttachmentPart(dh); attachment3.setContentId("gyro_image"); message.addAttachmentPart(attachment); // Get attachment Iterator it = message.getAttachments(); while (it.hasNext()) { AttachmentPart attachment4 = (AttachmentPart) it.next(); Object content = attachment4.getContent(); String id = attachment4.getContentId(); System.out.print("Attachment " + id + " contains: " + content); System.out.println(""); } pcCon.send(message); pcCon.close(); } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |