学习使用JAXM处理SOAP(转)

刚刚看完JAXM的教程,赶紧做个学习记录,以备以后的温习。

       我使用的是Myeclipse开发工具,包含了大部分jar包了,还差一个JAXM-API包需要另外加进来,我是在我安装的JEE5的文件夹里找到的。(在附件里供下载)

       使用JAXM发送SOAP有两种方法:SOAPConnection和Messaging providers,而编码SOAP的方法却是一样的,具体的还是在代码里体现吧。

    这段代码使用的是SOAPConnection:

Java代码  收藏代码
  1. public static void main(String[] args)  
  2.             throws UnsupportedOperationException, SOAPException {  
  3.           
  4.         // Create SOAPConnection, two steps:  
  5.         SOAPConnectionFactory soapFactory = SOAPConnectionFactory.newInstance();  
  6.         SOAPConnection con = soapFactory.createConnection();  
  7.   
  8.         // Create SOAPMessage  
  9.         MessageFactory messageFactory = MessageFactory.newInstance();  
  10.         SOAPMessage message = messageFactory.createMessage();  
  11.           
  12.                // Create SOAPPart  
  13.         SOAPPart soapPart = message.getSOAPPart();  
  14.         // Create SOAPEnvelope  
  15.         SOAPEnvelope soapEnvelope = soapPart.getEnvelope();  
  16.         // Create SOAPHeader  
  17.         SOAPHeader soapHeader = soapEnvelope.getHeader();  
  18.         soapHeader.detachNode();     //delete Header node  
  19.         // Create SOAPBody  
  20.         SOAPBody soapBody = soapEnvelope.getBody();  
  21.         Name bodyName = soapEnvelope.createName("getLastTradePrice", "m",  
  22.                 "http://http://smartzxy.iteye.com");  
  23.         SOAPBodyElement gltp = soapBody.addBodyElement(bodyName);  
  24.         // insert content to <getLastTradePrice/>  
  25.         Name name=soapEnvelope.createName("Symbol");  
  26.         SOAPElement symbol=soapBody.addChildElement(name);  
  27.         symbol.addTextNode("SUNW");  
  28.         try {  
  29.             //Print the packed SOAP message  
  30.                        message.writeTo(System.out);  
  31.         } catch (IOException e) {  
  32.                
  33.             e.printStackTrace();  
  34.         }  
  35.                  
  36.                //Send SOAP message  
  37.         URLEndpoint endpoint=new URLEndpoint("http://smartzxy.iteye.com");  
  38.         SOAPMessage response=con.call(message, endpoint);  
  39.         con.close();  
  40.               
  41.     }  

 

    这段代码使用的是Messaging Provider:

Java代码  收藏代码
  1. public static void main(String[] args) throws NamingException,  
  2.             SOAPException, ParserConfigurationException, MalformedURLException {  
  3.           
  4.         // Create Messaging Provider to send SOAP message  
  5.         Context ctx = new InitialContext();  
  6.         ProviderConnectionFactory pcFactory = (ProviderConnectionFactory) ctx  
  7.                 .lookup("Test1");  
  8.         ProviderConnection pcCon = pcFactory.createConnection();  
  9.   
  10.         // Get a Message Factory  
  11.         ProviderMetaData metaData = pcCon.getMetaData();  
  12.         String[] supportedProfiles = metaData.getSupportedProfiles();  
  13.         String profile = null;  
  14.         for (int i = 0; i < supportedProfiles.length; i++) {  
  15.             if (supportedProfiles[i].equals("ebxml")) {  
  16.                 profile = supportedProfiles[i];  
  17.                 break;  
  18.             }  
  19.         }  
  20.         MessageFactory factory = pcCon.createMessageFactory(profile);  
  21.   
  22.         // Create SOAP message  
  23.         EbXMLMessageImpl message=(EbXMLMessageImpl)factory.createMessage();  
  24.         SOAPMessage message = factory.createMessage();  
  25.           
  26.                SOAPPart soappart = message.getSOAPPart();  
  27.           
  28.   
  29.         // Create SOAP message from existed xml file  
  30.         DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();  
  31.         DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();  
  32.         try {  
  33.             Document doc = dBuilder.parse("e:\\soap.xml");  
  34.             DOMSource domSource = new DOMSource(doc);  
  35.             soappart.setContent(domSource);  
  36.         } catch (SAXException e) {  
  37.                
  38.             e.printStackTrace();  
  39.         } catch (IOException e) {  
  40.                
  41.             e.printStackTrace();  
  42.         }  
  43.   
  44.         // Add attachment  
  45.         String str = "Update address for Sunny Skies "  
  46.                 + "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";  
  47.         AttachmentPart attachment = message.createAttachmentPart();  
  48.         attachment.setContent(str, "text/plain");  
  49.         attachment.setContentId("address");  
  50.         message.addAttachmentPart(attachment);  
  51.   
  52.         //Add jpeg attachment   
  53.                AttachmentPart attachment2=message.createAttachmentPart();   
  54.                byte[] jpegData=…;   
  55.                ByteArrayInputString stream=new ByteArrayInputStream(jpegData);  
  56.         attachment2.setContent(stream,”image/jpeg”);  
  57.             message.addAttachmentPart(attachment);  
  58.           
  59.         //Another way to add jpeg attachment  
  60.                URL url = new URL("http://greatproducts.com/gizmos/img.jpg");  
  61.         DataHandler dh = new DataHandler(url);  
  62.         AttachmentPart attachment3 = message.createAttachmentPart(dh);  
  63.         attachment3.setContentId("gyro_image");  
  64.         message.addAttachmentPart(attachment);  
  65.   
  66.         // Get attachment  
  67.         Iterator it = message.getAttachments();  
  68.         while (it.hasNext()) {  
  69.             AttachmentPart attachment4 = (AttachmentPart) it.next();  
  70.             Object content = attachment4.getContent();  
  71.             String id = attachment4.getContentId();  
  72.             System.out.print("Attachment " + id + " contains: " + content);  
  73.             System.out.println("");  
  74.         }  
  75.   
  76.         pcCon.send(message);  
  77.         pcCon.close();  
  78.     } 
posted on 2017-07-27 11:13  xiaopangzhi  阅读(318)  评论(0)    收藏  举报