ITeye博客

AXIS2 RCP/literal样式,两种MESSAGE定义方式SOAP的payload

douh · 2010-08-02 14:27 · WEBSERVICE · 编程语言 · Markdown

没有COMPLEXTYPE的MESSAGE WSDL文件内容 清单1:


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://www.ctgx.com/RPCService/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="RPCService"
	targetNamespace="http://www.ctgx.com/RPCService/">
	<wsdl:message name="concatRequest">
		<wsdl:part name="s1" type="xsd:string" />
		<wsdl:part name="s2" type="xsd:string"/>
	</wsdl:message>
	<wsdl:message name="concatResponse">
		<wsdl:part name="out" type="xsd:string" />
	</wsdl:message>
	<wsdl:portType name="RPCService">
		<wsdl:operation name="concat">
			<wsdl:input message="tns:concatRequest" />
			<wsdl:output message="tns:concatResponse" />
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name="RPCServiceSOAP" type="tns:RPCService">
		<soap:binding style="rpc"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="concat">
			<soap:operation soapAction="http://www.ctgx.com/RPCService/concat" />
			<wsdl:input>
				<soap:body namespace="http://www.ctgx.com/RPCService/"
					use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body namespace="http://www.ctgx.com/RPCService/"
					use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="RPCService">
		<wsdl:port binding="tns:RPCServiceSOAP" name="RPCServiceSOAP">
			<soap:address location="http://localhost:8080/axis2/services/RPCService" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>
清单2:

客户端调用时,直接使用AXIOM(lowlevel)的代码:


package com.ctgx.www.rpcservice;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

/**
 * filename:LowLevelClient.java description: the file desciption goes here
 * Copyright: Copyright(c)2010 CTGX *
 * 
 * @author: 梁明杰
 * @version: 1.0 Create at: 2010-7-28 下午04:47:45
 * 
 */
public class LowLevelClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws AxisFault {
		ServiceClient client = new ServiceClient();
		Options options = new Options();
		//采用这种方式必须设定HTTP SOAPAction Header
		options.setAction("http://www.ctgx.com/RPCService/concat");
		options.setTo(new EndpointReference(
				"http://localhost:1234/axis2/services/RPCService?wsdl"));
		client.setOptions(options);
		OMElement request = makeRequest();
		OMElement response = client.sendReceive(request);
		System.out.println(response.toString());

	}

	private static OMElement makeRequest() {
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMElement request = factory.createOMElement(new QName(
				"http://www.ctgx.com/RPCService/", "concat"));
		OMElement s1 = factory.createOMElement(new QName("s1"));
		s1.setText("abc");
		OMElement s2 = factory.createOMElement(new QName("s2"));
		s2.setText("123");
		request.addChild(s2);
		request.addChild(s1);
		return request;

	}

}


对应的SOAP消息如下: 清单3:


<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Body>
         <axis2ns1:concat xmlns:axis2ns1="http://www.ctgx.com/RPCService/">
            <s2>123</s2>
            <s1>abc</s1>
         </axis2ns1:concat>
      </soapenv:Body>
   </soapenv:Envelope>

使用COMPLEXTYPE的MESSAGE WSDL文件内容 清单4:


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://www.ctgx.com/SimpleService1/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="SimpleService1"
	targetNamespace="http://www.ctgx.com/SimpleService1/">
	<wsdl:types>
		<xsd:schema targetNamespace="http://www.ctgx.com/SimpleService1/">
			<xsd:complexType name="concat">
				<xsd:sequence>
					<xsd:element name="s1" type="xsd:string" />
					<xsd:element name="s2" type="xsd:string"></xsd:element>
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="concatResponse">
				<xsd:sequence>
					<xsd:element name="out" type="xsd:string" />
				</xsd:sequence>
			</xsd:complexType>
		</xsd:schema>
	</wsdl:types>
	<wsdl:message name="concatRequest">
		<wsdl:part type="tns:concat" name="parameters" />
	</wsdl:message>
	<wsdl:message name="concatResponse">
		<wsdl:part type="tns:concatResponse" name="parameters" />
	</wsdl:message>
	<wsdl:portType name="SimpleService1">
		<wsdl:operation name="concat">
			<wsdl:input message="tns:concatRequest" />
			<wsdl:output message="tns:concatResponse" />
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name="SimpleServiceSOAP" type="tns:SimpleService1">
		<soap:binding style="rpc"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="concat">
			<soap:operation soapAction="http://www.ctgx.com/SimpleService1/concat" />
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="SimpleService">
		<wsdl:port binding="tns:SimpleServiceSOAP" name="SimpleServiceSOAP">
			<soap:address location="http://localhost:8080/axis2/services/SimpleService1" />
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>


客户端测试代码: 清单5:


package com.ctgx.www.simpleservice1;

import javax.xml.namespace.QName;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axis2.AxisFault;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;

/**
 * filename:LowLevelClient.java description: the file desciption goes here
 * Copyright: Copyright(c)2010 CTGX *
 * 
 * @author: 梁明杰
 * @version: 1.0 Create at: 2010-7-28 下午04:47:45
 * 
 */
public class LowLevelClient {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws AxisFault {
		ServiceClient client = new ServiceClient();
		Options options = new Options();
		options.setAction("http://www.ctgx.com/SimpleService1/concat");
		options.setTo(new EndpointReference(
				"http://localhost:1234/axis2/services/SimpleService1"));
		client.setOptions(options);
		OMElement request = makeRequest();
		OMElement response = client.sendReceive(request);
		System.out.println(response.toString());

	}

	private static OMElement makeRequest() {
		OMFactory factory = OMAbstractFactory.getOMFactory();
		OMElement request = factory.createOMElement(new QName(
				"http://www.ctgx.com/SimpleService1/", "concat"));
		OMElement parameters=factory.createOMElement(new QName("parameters"));
		OMElement s1 = factory.createOMElement(new QName("s1"));
		s1.setText("abc");
		OMElement s2 = factory.createOMElement(new QName("s2"));
		s2.setText("123");
		request.addChild(parameters);
		parameters.addChild(s1);
		parameters.addChild(s2);
		return request;

	}

}


SOAP消息 清单6:


 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Body>
         <axis2ns1:concat xmlns:axis2ns1="http://www.ctgx.com/SimpleService1/">
            <parameters>
               <s1>abc</s1>
               <s2>123</s2>
            </parameters>
         </axis2ns1:concat>
      </soapenv:Body>
   </soapenv:Envelope>

需要说明的地方: 第一,清单1没有使用COMPLEXTYPE,清单4使用了COMPLEXTYPE 所以导致清单3和清单6的SOAP BODY 的消息不同。清单3中两个参数S1和S2直接包含在方法名concat中,且名字和MESSAGE的PART的名字相同。清单6中两个参数S1和S2包含在parameters中,parameters比如和MESSAGE part的名字,且PARAMETERS的两个元素就是COMPLEXTYPE中定义的元素。在SOAP中不能有类型和元素的引用,PARAMETERS实际上就构成了一个复合元素。 第二,清单3和清单6中S1和S2的顺序不同,这和AXIS2生成的服务端代码有关系。也就是说,MESSAGE中的PART顺序,不一定是方法中参数的顺序,不过使用COMPLEXTYPE方式定义,可以保证这一点。这和AXIS2的数据绑定有关系。 第三点,AXIS2对RCP/LITERAL的支持,实际上是DOCOMENT WRAPPER方式,也就是说,在RCP/LITERAL代码生成过程中,AXIS2做包装转换 附件是TCPMON应用程序,用来捕捉SOAP信息非常方便

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。