SOAP是把基于HTTP的WEB技术与XML的灵活性和可扩展性组合在了一起。
准备:
1. 准备一个web server。Tomcat 下载地址 http://tomcat.apache.org/
本例使用版本 apache-tomcat-7.0.54-windows-x64.zip
2. 写一个servlet send用来发送一个SOAP的message并添加一个附件。
3. 写一个servlet receive用来做SOAP message的解析并返回一个response。
4. 写一个简单的html页面触发 servlet A。
开发工具:
eclipse 版本 eclipse-jee-juno-SR2-win32-x86_64
下载地址:
http://www.eclipse.org/downloads/
下载J2EE版本的eclipse里面包含了SOAP常用的一些jar,但这个版本的没有jaxm-api.jar,这个需要额外下载
然后倒入。
代码结构:
web.xml代码如下:
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd"> <web-app> <servlet> <servlet-name>DemoSend</servlet-name> <servlet-class>cody.soap.demo.DemoSend</servlet-class> </servlet> <servlet> <servlet-name>DemoReceiver</servlet-name> <servlet-class>cody.soap.demo.DemoReceiver</servlet-class> </servlet> <servlet-mapping> <servlet-name>DemoSend</servlet-name> <url-pattern>/DemoSend</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DemoReceiver</servlet-name> <url-pattern>/DemoReceiver</url-pattern> </servlet-mapping> </web-app>
DemoSend.java
package cody.soap.demo; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.net.URL; import javax.activation.DataHandler; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.xml.soap.AttachmentPart; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPHeader; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; public class DemoSend extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; private SOAPConnection connection; @Override public void init() throws ServletException { super.init(); try { SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance(); connection = connectionFactory.createConnection(); } catch (UnsupportedOperationException e) { e.printStackTrace(); } catch (SOAPException e) { e.printStackTrace(); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String outString ="<HTML><H1>Sending and reading the SOAP Message</H1><P>"; try { MessageFactory messageFactory = MessageFactory.newInstance(); SOAPMessage outgoingMessage = messageFactory.createMessage(); SOAPPart soappart = outgoingMessage.getSOAPPart(); SOAPEnvelope envelope = soappart.getEnvelope(); SOAPHeader header = envelope.getHeader(); SOAPBody body = envelope.getBody(); body.addBodyElement(envelope.createName("numberAvailable", "laptops", "http://ecodl.taobao.com/")).addTextNode("216"); StringBuffer serverUrl = new StringBuffer(); serverUrl.append(request.getScheme()).append("://").append(request.getServerName()); serverUrl.append(":").append(request.getServerPort()).append(request.getContextPath()); String baseUrl = serverUrl.toString(); URL url = new URL(baseUrl + "/test.html"); AttachmentPart attachmentpart = outgoingMessage.createAttachmentPart(new DataHandler(url)); attachmentpart.setContentType("text/html"); outgoingMessage.addAttachmentPart(attachmentpart); URL client = new URL(baseUrl + "/DemoReceiver"); FileOutputStream outgoingFile = new FileOutputStream("out.msg"); outgoingMessage.writeTo(outgoingFile); outgoingFile.close(); outString += "SOAP outgoingMessage sent (see out.msg). <BR>"+baseUrl+"<BR>"; SOAPMessage incomingMessage = connection.call(outgoingMessage, client); if (incomingMessage != null) { FileOutputStream incomingFile = new FileOutputStream("in.msg"); incomingMessage.writeTo(incomingFile); incomingFile.close(); outString += "SOAP outgoingMessage received (see in.msg).</HTML>"; } } catch (SOAPException e) { e.printStackTrace(); } try { OutputStream outputStream = response.getOutputStream(); outputStream.write(outString.getBytes()); outputStream.flush(); outputStream.close(); } catch (IOException e) {} } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
DemoReceiver.java
package cody.soap.demo; import java.util.Iterator; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.xml.messaging.JAXMServlet; import javax.xml.messaging.ReqRespListener; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPElement; import javax.xml.soap.SOAPEnvelope; import javax.xml.soap.SOAPException; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPPart; public class DemoReceiver extends JAXMServlet implements ReqRespListener{ /** * Generated automatically */ private static final long serialVersionUID = 1L; private static MessageFactory messageFactory = null; @Override public void init(ServletConfig servletConfig) throws ServletException { super.init(servletConfig); try { messageFactory = MessageFactory.newInstance(); } catch (SOAPException e) { e.printStackTrace(); } } @Override public SOAPMessage onMessage(SOAPMessage msg) { SOAPPart soappart = msg.getSOAPPart(); try { SOAPEnvelope incomingEnvelope; incomingEnvelope = soappart.getEnvelope(); SOAPBody body = incomingEnvelope.getBody(); Iterator<?> iterator = body.getChildElements(incomingEnvelope.createName("numberAvailable", "laptops", "http://ecodl.taobao.com/")); SOAPElement element; element = (SOAPElement) iterator.next(); SOAPMessage message = messageFactory.createMessage(); SOAPEnvelope envelope = message.getSOAPPart().getEnvelope(); SOAPBody responsebody = envelope.getBody(); String responseText = "Got the SOAP message indicating there are " + element.getValue() + " laptops available."; responsebody.addChildElement(envelope.createName("Response")).addTextNode(responseText); return message; } catch (SOAPException e) { e.printStackTrace(); return null; } } }
test.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <HEAD> <TITLE>SOAP and Java</TITLE> </HEAD> <BODY> <H1>SOAP and Java</H1> Click <A HREF="DemoSend">here</a> to send the SOAP message. </BODY> </HTML>
out.msg
------=_Part_0_1985246804.1403343570084 Content-Type: text/xml; charset=utf-8 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <laptops:numberAvailable xmlns:laptops="http://ecodl.taobao.com/">216</laptops:numberAvailable> </SOAP-ENV:Body> </SOAP-ENV:Envelope> ------=_Part_0_1985246804.1403343570084 Content-Type: text/html <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML> <HEAD> <TITLE>SOAP and Java</TITLE> </HEAD> <BODY> <H1>SOAP and Java</H1> Click <A HREF="DemoSend">here</a> to send the SOAP message. </BODY> </HTML> ------=_Part_0_1985246804.1403343570084--
in.msg
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"> <SOAP-ENV:Header/> <SOAP-ENV:Body> <Response>Got the SOAP message indicating there are 216 laptops available. </Response> </SOAP-ENV:Body> </SOAP-ENV:Envelope>
原文:http://www.informit.com/library/content.aspx?b=STY_XML_21days&seqNum=228
在Tomcat上部署的目录结构:
webapps [This is a directory] |____soapdemoserver [This is a directory] |____test.html [Our starting Web page] |____WEB-INF [This is a directory] |____web.xml [Configures Tomcat] |____classes [This is a directory] |____cody/soap/demo [This is a directory] |____DemoReceiver.class [The server servlet] |____DemoSend.class [The client servlet]
相关推荐
SOAP(Simple Object ...总的来说,了解SOAP协议、如何在Java中实现SOAP请求以及解析WSDL文件是Web服务开发的关键技能。通过实践和学习这些知识点,开发者能够创建和消费跨平台的Web服务,实现不同系统的互操作性。
Java中的SOAP实现通常依赖于JAX-WS(Java API for XML Web Services)框架,它提供了一种简单的方式来创建和消费SOAP服务。JAX-WS包括几个关键组件:Service端点接口(SEI)、服务实现、WSDL(Web服务描述语言)和...
【标题】"cxf_helloworld.rar" 是一个关于Apache CXF框架入门的示例项目,它专注于SOA(Service-Oriented Architecture,面向服务架构)的实现,特别是使用Java技术。"SOA CXF_SOA JAVA_cxf_cxf helloworld_hello...
在本教程中,我们将探讨“webservice复习-helloworld”这个主题,它是一个基础的Web服务示例,帮助开发者理解和掌握Web服务的基本概念和实现。 首先,Web服务的核心标准是SOAP(Simple Object Access Protocol),...
本示例将引导你了解如何使用Apache CXF创建一个简单的“Hello World”应用程序,涉及客户端和服务端的实现。 首先,让我们从服务端(WS_Server)开始。在CXF中,服务端通常被称为服务提供者。为了创建一个服务,你...
总之,使用CXF框架在SOAP协议下开发Web服务,不仅可以享受到Java语言的强大功能,还能充分利用CXF提供的便利性和灵活性。通过深入学习和实践,开发者可以高效地构建跨平台、安全可靠的分布式系统。
Axis1利用SOAP协议,使得开发者能够方便地创建、发布和调用Web服务。 创建"Hello, World!" Web服务的第一步是编写Java代码。我们需要一个简单的Java类,该类包含一个公共方法,例如`sayHello()`,该方法返回字符串...
Apache CXF是一个开源的Java框架,它用于构建和开发Web服务,支持SOAP、RESTful等多种通信协议。这个"helloWorld"示例是学习CXF入门的典型教程,旨在帮助开发者理解如何通过CXF快速地搭建服务并进行交互。 【描述】...
在这个"apache soap的hello例子"中,我们将探讨如何使用Apache SOAP实现一个简单的“Hello World”服务。 首先,让我们理解SOAP的基本结构。SOAP消息通常包含三个主要部分:Header、Body和Envelope。Header用于传递...
在这个“cxf实用案例代码helloworld”中,我们将深入探讨如何使用CXF来实现一个简单的 HelloWorld 示例,这对于初学者理解CXF的工作原理非常有帮助。 一、CXF简介 Apache CXF是基于Java的Web服务开发框架,它提供...
在这个“webservices soap (java)实例”中,我们将探讨如何使用Java来实现SOAP Web服务,特别是通过使用XFire框架。 XFire是一个已不再维护的Java库,它简化了SOAP Web服务的创建和消费。尽管现在更多的人选择使用...
在"HelloWorld WebService Server"这个例子中,服务器端程序是实现Web服务的核心部分。通常,我们需要选择一种编程语言(如Java、Python或.NET)和相应的框架(如Apache CXF、Spring Boot或Flask)来开发。例如,...
在提供的文件中,“HelloWorld.java”可能是一个简单的Java应用程序,它实现了SOAP Web服务的服务器端。在Java中,我们可以使用JAX-WS(Java API for XML Web Services)来创建SOAP服务。服务端通常会定义一个接口...
【标题】"CXF视频:1、使用CXF实现简单的HelloWorld",这是一段关于Apache CXF框架的初级教程,旨在引导初学者通过实际操作理解如何利用CXF创建一个基本的“HelloWorld”服务。Apache CXF是一个开源的Java框架,它...
描述中提到了"WebService之WSDL和SOAP实例",这进一步说明了我们不仅在谈论SOAP,还涉及到WSDL(Web Services Description Language)。WSDL是XML格式的文档,用于描述Web服务及其接口。当创建一个SOAP服务时,通常...
SOAP与Java编程指南将涵盖上述所有概念,从理解SOAP消息结构到实现在Java环境中创建和使用SOAP服务。同时,强调了对WEB前台基础和AJAX的理解,因为这有助于优化SOAP服务在客户端的使用,提升用户体验。通过深入学习...
在我们的"helloWorld"项目中,可能包含了服务接口、实现类、配置文件以及可能的测试客户端代码。通过这个简单的DEMO,你可以了解到如何使用Apache CXF构建Web服务,并进行基本的交互。随着对CXF的进一步学习,你还...
标题 "我的webservice Hello world-axis" 指的是一个基于Apache Axis实现的Web服务示例,主要用于初学者学习和理解Web服务的基本概念。Apache Axis是一个开源的SOAP(简单对象访问协议)工具包,用于创建和部署Web...