`
smartzxy
  • 浏览: 195458 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

学习使用JAXM处理SOAP

阅读更多

       刚刚看完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
分享到:
评论
1 楼 duoluoboy365 2008-10-29  
你好,就是看了你的文章后,很不错。。
可是我在模仿时,EbXMLMessageImpl类不是JAXMapi中的一个类,但是我找了很长时间也没有找到这类所在哪个包中,
新手指点一下?
changu2006@126.com
谢谢

相关推荐

    使用SAAJ 和JAXM的 SOAP客户端及服务

    使用SAAJ 和JAXM的 SOAP客户端及服务--学习文档

    一个JAXM小实验:实现SOAP信息的收发

    我生成的war文件 博文链接:https://smartzxy.iteye.com/blog/220172

    Java SOAP协议

    本文的预定作者应该对SOAP1.1的基本规范有所了解,并熟悉了j2ee的基本开发,如果不熟悉的话,可以看一下我的Blog:使用SOAP开发java web服务--Axis开发方案 ,详细标准可以查看w3c的官方网站,连接如下:...

    简述JAXM构建Web服务及应用

    JAXM的基本概念包括消息,连接,消息提供者以及JAXM客户端和JAXM服务消息,JAXM的消息使用SOAP消息标准,包括或不包括附件。  1 JAXM相关概念  完整的JAXM API存在于javax.xml.soap和javax.xml.messaging二个...

    jaxm-1_1_2.zip

    jaxm-1_1_2.zip是java的IAP,但是我们安装的JDK版本的API里面查不到这个包,提供给需要的同学!

    通信与网络中的简述JAXM构建Web服务及应用

    JAXM的基本概念包括消息,连接,消息提供者以及JAXM客户端和JAXM服务消息,JAXM的消息使用SOAP消息标准,包括或不包括附件。  1 JAXM相关概念  完整的JAXM API存在于javax.xml.soap和javax.xml.messaging二个...

    jaxm-api.jar

    这个包很难找啊,没什么资源,用于XML流处理信息的

    基于JAXM构建Web服务及应用

    介绍了JAXM结构,并利用其特点构建了不使用消息接发提供者的Web服务应用,分析了基于JAXM设计的体系结构和设计模式。

    Java Web Service 程序例子

    Java Web Service 学习例子,里面有大量用来开发web services的Java程序示例,非常值得学习。 使用到的技术及工具如下: Apache: www.apache.org and xml.apache.org -- Ant 1.4.1 -- SOAP 2.2 -- Tomcat 3.2...

    J2EE笔试题

    1.MVC的各个部分都有那些技术来实现?如何实现? 2.J2EE是什么? 3.J2EE是技术还是平台还是框架? 4.STRUTS的应用(如STRUTS架构) ...JAXP、JAXM的解释。SOAP、UDDI,WSDL解释。 6.C/S 与 B/S 区别: 。。。。。。

    Java Web 服务开发-值得一看

    JAXM(Java API for XML Messaging)|SAAJ(SOAP with Attachement API for Java) Java Web服务环境 商业的 IBM Websphere,WebLogic… 开源的 AXIS,Jar数量适中 AXIS2,Jar文件太多,不容易分清,不能全加入classpath ...

    XML解析教程

    如今你可以看到越来越多的Java软件都在使用DOM4J来读写XML,特别值得一提的是连Sun的JAXM(Java API for XML Messaging )也在用DOM4J。目前许多开源项目中大量采用DOM4J,例如大名鼎鼎的Hibernate也用DOM4J来读取XML...

    Java Web Services

    Java Web Services shows you how to use SOAP to perform remote method calls and message passing;how to use WSDL to describe the interface to a web service or understand the interface of someone else's ...

    java web services

    Java Web Services shows you how to use SOAP to perform remote method calls and message passing; how to use WSDL to describe the interface to a web service or understand the interface of someone else's...

    java面试题

    常见java面试题 MVC的各个部分都有那些技术来实现?如何实现? WEB SERVICE名词解释。JSWDL开发包的介绍。JAXP、JAXM的解释。SOAP、UDDI,WSDL解释。 什么是JNDI

    JAVA的WebService支持CXF与SAAJ第三版

    这个教程将了如何使用CXF开发WebService,包括JAX-WS、JAX-RS规范、数字证书、MTOM附件传输、异步调用、异常处理、使用Axis+Spring...中间的附件处理部分又增加了详细的描述,最后部分增加了JAXM&SAAJ规范的使用示例。

    DOM4J的jar包和API

    Dom4j是一个简单、灵活的开放源代码的库。Dom4j是由早期开发JDOM的人分离出来而后独立开发...现在很多软件采用的Dom4j,例如Hibernate,包括sun公司自己的JAXM也用了Dom4j。 使用Dom4j开发,需下载dom4j相应的jar文件。

    dom4j文档和源码及jar包

    dom4j是一个Java的XML API,类似于jdom,用来读写XML文件...如今你可以看到越来越多的Java软件都在使用dom4j来读写XML,特别值得一提的是连Sun的JAXM也在用dom4j。这是必须使用的jar包, Hibernate用它来读写配置文件。

Global site tag (gtag.js) - Google Analytics