论坛首页 Java企业应用论坛

学习使用JAXM处理SOAP

浏览 3522 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-07-18  
SOA

       刚刚看完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();
	}
 
  • jaxm-api.jar (22.5 KB)
  • 描述: 用到的jar包:jaxm-api.jar
  • 下载次数: 493
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics