`
itwangxinli
  • 浏览: 146062 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

用Java来输出soap报文

阅读更多

   前段时间开发ACS,是基于SOAP协议的通信,一次通信过程包含多个soap报文,而且也不想普通的webserivice那样,

 soap报文是自动生成的。ACS的通信的报文是硬编码编出来的,虽然能正确运行,但是实在是不雅,最近闲来无事,

 想着如何用像webservice那样,用Java对象设置参数后,将对象转换成String格式的SOAP报文,这样以后程序的维护

 问题就得到解决了。

  然后在google中baidu了一把,找了个例子,然后开始试验了。其实程序并不难,写这个文章,主要是帮助那些直接使用SOAP

协议进行开发的朋友们参考,也给自己留个备注,呵呵。

package com.seahigh.acs.soap;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
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.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
/**
 * 
 * @author 汪心利
 * Date 2010-3-30下午02:51:30
 * (c)CopyRight seahigh 2010
 */
public class SoapUtil {
	public SOAPPart initSoapPart() throws SOAPException {

		SOAPMessage soapMessage = MessageFactory.newInstance().createMessage();

		SOAPPart soapPart = soapMessage.getSOAPPart();

		SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
		SOAPHeader soapHeader = soapEnvelope.getHeader();
		SOAPElement cwmp = soapEnvelope.addNamespaceDeclaration("cwmp",
				"urn:dslforum-org:cwmp-1-0");
		SOAPElement xsi = soapEnvelope.addNamespaceDeclaration("xsi",
				"http://www.w3.org/2001/XMLSchema-instance");
		SOAPElement xsd = soapEnvelope.addNamespaceDeclaration("xsd",
				"http://www.w3.org/2001/XMLSchema");

		SOAPElement enc = soapEnvelope.addNamespaceDeclaration("SOAP-ENC",
				"http://schemas.xmlsoap.org/soap/encoding/");

		SOAPElement id = soapHeader.addChildElement("ID", "cwmp");
		id.setTextContent("1");
		return soapPart;
	}

	public void soap2String(Source source) throws Exception {
		if (source != null) {
			Node root = null;
			if (source instanceof DOMSource) {
				root = ((DOMSource) source).getNode();
			} else if (source instanceof SAXSource) {
				InputSource inSource = ((SAXSource) source).getInputSource();
				DocumentBuilderFactory dbf = DocumentBuilderFactory
						.newInstance();
				dbf.setNamespaceAware(true);
				Document doc = dbf.newDocumentBuilder().parse(inSource);
				root = (Node) doc.getDocumentElement();
			}
			Transformer transformer = TransformerFactory.newInstance()
					.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.transform(new DOMSource(root), new StreamResult(
					System.out));
		}
	}

	public Source informResponse(SOAPPart part) throws Exception {
		SOAPEnvelope soapEnvelope = part.getEnvelope();
		SOAPBody soapBody = soapEnvelope.getBody();
		SOAPElement informRes = soapBody.addChildElement("InformResponse",
				"cwmp");
		SOAPElement max = SOAPFactory.newInstance().createElement(
				"MaxEnvelopes", "", "");
		max.setTextContent("1");
		informRes.addChildElement(max);
		return part.getContent();
	}

	public Source getParameterValues(SOAPPart part, List list) throws Exception {
		SOAPEnvelope soapEnvelope = part.getEnvelope();
		SOAPBody soapBody = soapEnvelope.getBody();
		SOAPElement informRes = soapBody.addChildElement("GetParameterValues",
				"cwmp");
		SOAPFactory soapFactory = SOAPFactory.newInstance();
		SOAPElement names = soapFactory.createElement("ParameterNames", "", "");
		names.addAttribute(new QName("SOAP-ENC:arrayType"), "xsd:string["
				+ list.size() + "]");
		SOAPElement nameElement = null;
		for (int i = 0; i < list.size(); i++) {
			nameElement = soapFactory.createElement("string", "", "");
			nameElement.setTextContent((String) list.get(i));
			names.addChildElement(nameElement);
		}
		informRes.addChildElement(names);
		return part.getContent();
	}

	public static void main(String[] args) throws Exception {
		SoapUtil util = new SoapUtil();
		SOAPPart part = util.initSoapPart();
		Source inform = util.getParameterValues(part, util.getTestnames());
		util.soap2String(inform);
	}

	public List<String> getTestnames() {
		List<String> list = new ArrayList<String>();
		list.add("InternetGatewayDevice.DeviceInfo.X_CT-COM_CPU");
		list.add("InternetGatewayDevice.DeviceInfo.X_CT-COM_WorkTime");
		list.add("InternetGatewayDevice.DeviceInfo.SoftwareVersion");
		list.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_ReceiveNoise");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalBytesReceived");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalBytesSent");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalBytesReceived");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalBytesSent");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalPacketsReceived");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalPacketsSent");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalPacketsReceived");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalPacketsSent");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.ResponsePass");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.AskPass");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.SuccessPass");
		list.add("InternetGatewayDevice.DeviceInfo.X_CT-COM_Temp");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.LAN-PacketsError");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.LAN-TotalBytesReceived");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.LAN-TotalBytesSent");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.WAN-TotalBytesReceived");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.WAN-PacketsError");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.WAN-TotalBytesSent");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.AssociatedDevice.1.X_CT-COM_ReceiveRate");
		list
				.add("InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.AssociatedDevice.1.X_CT-COM_SendRate");
		list.add("InternetGatewayDevice.DeviceInfo.SerialNumber");
		list.add("InternetGatewayDevice.DeviceInfo.ManufacturerOUI");
		return list;
	}
}

 

这就是研究的成果了,看下结果:

对应TR069协议中的RCP Method:GetParameterValues

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header>
<cwmp:ID>1</cwmp:ID>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<cwmp:GetParameterValues>
<ParameterNames SOAP-ENC:arrayType="xsd:string[27]">
<string>InternetGatewayDevice.DeviceInfo.X_CT-COM_CPU</string>
<string>InternetGatewayDevice.DeviceInfo.X_CT-COM_WorkTime</string>
<string>InternetGatewayDevice.DeviceInfo.SoftwareVersion</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_ReceiveNoise</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalBytesReceived</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalBytesSent</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalBytesReceived</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalBytesSent</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalPacketsReceived</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_LAN-TotalPacketsSent</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalPacketsReceived</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_WAN-TotalPacketsSent</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.ResponsePass</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.AskPass</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.SuccessPass</string>
<string>InternetGatewayDevice.DeviceInfo.X_CT-COM_Temp</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.LAN-PacketsError</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.LAN-TotalBytesReceived</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.LAN-TotalBytesSent</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.WAN-TotalBytesReceived</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.WAN-PacketsError</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_CT-COM_Stat.WAN-TotalBytesSent</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.AssociatedDevice.1.X_CT-COM_ReceiveRate</string>
<string>InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.AssociatedDevice.1.X_CT-COM_SendRate</string>
<string>InternetGatewayDevice.DeviceInfo.SerialNumber</string>
<string>InternetGatewayDevice.DeviceInfo.ManufacturerOUI</string>
</ParameterNames>
</cwmp:GetParameterValues>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 CPE InformResponse

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:cwmp="urn:dslforum-org:cwmp-1-0" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Header>
<cwmp:ID>1</cwmp:ID>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<cwmp:InformResponse>
<MaxEnvelopes>1</MaxEnvelopes>
</cwmp:InformResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

 

1
0
分享到:
评论

相关推荐

    JAVA输出soap报文.pdf

    在 JAVA 中输出 SOAP 报文需要使用 javax.xml.soap 包中的类,主要包括 MessageFactory、SOAPMessage、SOAPPart、SOAPEnvelope、SOAPBody、SOAPHeader 等。 首先,需要使用 MessageFactory 创建一个 SOAPMessage ...

    JAVA输出soap报文[参考].pdf

    JAVA输出SOAP报文详解 SOAP(Simple Object Access Protocol)是一种基于XML的协议,用于在不同的操作系统和编程语言之间交换结构化信息。SOAP报文是SOAP协议的核心组件,是一种轻量级的、基于XML的协议,可以在...

    CXF打印SOAP报文,记录WebService日志

    通过上述步骤,我们成功实现了使用CXF打印SOAP报文的功能,并能够有效地记录WebService的日志信息。这对于调试和维护基于CXF的服务来说是非常有用的。此外,通过扩展此类自定义拦截器,还可以添加更多的功能,如安全...

    SOAP xml解析

    SOAP解析,与一般的xml解析不一样,他中间存在报文,解析到就出错了,也就解析不下去,这里举了一个列子,(网络上找了好久都没找到我想要的列子,最后我自己根据例子做了整理做出来的),把数据组合成集合。...

    通过webservice提交xml数据以及soap协议的使用

    在本主题中,我们将深入探讨如何通过Web服务提交XML数据以及SOAP(Simple Object Access Protocol)协议的使用。 XML(Extensible Markup Language)是Web服务中常用的数据交换格式,它是一种自描述、结构化的文本...

    接口输入报文

    ### 知识点详解:Java WebService 接口输入报文解析 #### 一、概述 在当前数字化时代,Web服务已经成为系统间交互的重要手段之一。WebService作为一种分布式系统的标准技术,广泛应用于各种业务场景中。Java作为...

    CXF 文件记录报文日志,非控制台打印。

    CXF使用Java的日志抽象层,如Log4j、Java Util Logging (JUL) 或者 Commons Logging。这意味着你可以选择任意一种支持的日志框架来配置日志输出。例如,如果你选择Log4j,你需要在项目中包含Log4j的库,并配置相应的...

    JAVA对象转换成XML(CDATA)

    为了解决这个问题,我们可以使用CDATA(Character Data)段来封装这些文本,使得XML解析器将其视为纯文本处理,不进行任何解析。 CDATA的定义格式是 `&lt;![CDATA[ 文本内容 ]]&gt;`,其中"文本内容"部分不能包含字符串"]...

    wsdl-报文模板读取(生成类似soapui打开wsdl模板样式)

    在IT行业中,Web服务接口的描述语言(WSDL,Web Services Description Language)是定义应用程序如何通过网络进行...这个过程涉及到XML解析、SOAP报文构造以及GUI编程等多个技术领域,对提升全面的IT技能有显著的帮助。

    Java与XML数据绑定

    例如,在SOAP Web服务中,JAXB常用于XML报文与Java对象的转换。 总之,Java与XML数据绑定是现代软件开发中的重要技能,它简化了XML处理,提高了代码的可读性和维护性。掌握JAXB和其他数据绑定技术,有助于开发者更...

    Java完整的Web Service示例代码,学会做web service程序

    4. **发布WSDL**:服务发布后,会自动生成一个WSDL文件,描述了服务的接口、方法、输入输出等信息,供客户端使用。 5. **启动服务器**:最后,启动服务端应用,使Web Service可供客户端调用。 在Java中,现代的Web...

    Java访问WebService返回XML数据的方法

    在实际应用中,这部分可能需要使用`java.net.URL`, `URLConnection`或者第三方库如Apache HttpClient来实现。 一旦解析得到`Document`对象,我们可以对其进行操作,例如查找、修改、遍历XML节点。在本例中,`hello...

    基于spring+cxf实现用户文件传输的webservice

    在完成接口的定义之后,我们可以开始编写Java类来实现上述接口。在实现接口时,我们使用@WebService指定服务接口的路径。 用户身份验证 为了确保安全性,我们还需要对用户身份进行基本认证。在Webservice中,我们...

    Java笔试题汇总(125道企业常见java笔试题)

    Java作为一门广泛使用的编程语言,深受企业和开发者喜爱。在求职过程中,尤其是对于初入IT行业的求职者,Java笔试题是衡量技术能力的重要标准之一。本资料"Java笔试题汇总(125道企业常见java笔试题)"包含了125个...

    CXF 服务端简单实例

    3. 注解服务:使用JAX-WS(Java API for XML Web Services)注解,如`@WebService`,来标记服务接口和实现类,这样CXF才能识别它们。 4. 创建服务端点:通过CXF的`JAXWSServerFactoryBean`或类似工具,配置并启动...

    ESB接入接出技术规范(超实用)

    在SOAP Header传递中,我们可以使用Java语言来编写客户端代码,例如: POST /com.hxlife.esb.inner.kernel.uWTrans0010 HTTP/1.1 Content-Type: text/xml; charset=utf-8 User-Agent: Java/1.6.0_45 Host: 10.0.14....

    SpringBoot WebService cxf接口发布以及logbok日志集成

    CXF(Apache CXF)是一个开源的Java服务框架,它允许开发者以多种协议如SOAP和RESTful来构建和部署Web服务。在SpringBoot中,我们可以通过添加cx-fusion starter依赖来启用CXF功能。接着,我们需要定义服务接口和...

    webservice

    对于客户端,通常使用SOAP库(如Java的JAX-WS或.NET的SOAP客户端)来生成请求并处理响应。客户端根据WSDL生成代理类,通过这些类与Web服务进行交互。 总的来说,Web服务是一种强大的工具,用于构建分布式系统和实现...

    webservice接口有关

    WebService接口定义了客户端如何与服务器端进行通信,它通常使用SOAP(Simple Object Access Protocol)协议来传输数据,而XML则作为数据的表示格式。XML因其结构化和可扩展性,使得跨平台的数据交换变得简单且易于...

    webservice实例

    2. 创建客户端:根据WSDL文件创建客户端代码,可以使用各种编程语言的WebService客户端库,如Java的JAX-WS,.NET的SoapHttpClient等。 3. 调用服务:在客户端代码中调用服务方法,传递必要的参数,执行用户管理操作...

Global site tag (gtag.js) - Google Analytics