微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Webservice_20_SOAP的基于契约优先头信息处理隐式头信息

非常感谢孙浩老师。

修改wsdl:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:deFinitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
	xmlns:tns="http://www.example.org/mywsdl/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
	xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="MyServiceImplService"
	targetNamespace="http://www.example.org/mywsdl/">
	<wsdl:types>
	
		<xsd:schema targetNamespace="http://www.example.org/mywsdl/">
			<xsd:element name="add" type="tns:addType" />
			<xsd:element name="addResponse" type="tns:addResponseType" />
			<xsd:element name="minus" type="tns:minusType" />
			<xsd:element name="minusResponse" type="tns:minusResponseType" />
			<!-- head -->
			<xsd:element name="licenseInfo" type="xsd:string" />

			<xsd:complexType name="addType">
				<xsd:sequence>
					<xsd:element name="a" type="xsd:int" />
					<xsd:element name="b" type="xsd:int" />
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="addResponseType">
				<xsd:sequence>
					<xsd:element name="addResult" type="xsd:int" />
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="minusType">
				<xsd:sequence>
					<xsd:element name="num1" type="xsd:int" />
					<xsd:element name="num2" type="xsd:int" />
				</xsd:sequence>
			</xsd:complexType>
			<xsd:complexType name="minusResponseType">
				<xsd:sequence>
					<xsd:element name="minusResult" type="xsd:int" />
				</xsd:sequence>
			</xsd:complexType>

		</xsd:schema>
	</wsdl:types>

	<wsdl:message name="add">
		<wsdl:part element="tns:add" name="add" />
	</wsdl:message>
	<wsdl:message name="addResponse">
		<wsdl:part element="tns:addResponse" name="addResponse" />
	</wsdl:message>
	<wsdl:message name="minus">
		<wsdl:part element="tns:minus" name="minus" />
	</wsdl:message>
	<wsdl:message name="minusResponse">
		<wsdl:part element="tns:minusResponse" name="minusResponse" />
	</wsdl:message>
	<!-- head -->
	<wsdl:message name="licenseInfo">
		<wsdl:part name="licenseInfo" element="tns:licenseInfo"></wsdl:part>
	</wsdl:message>

	<wsdl:portType name="IMyService">
		<wsdl:operation name="add">
			<wsdl:input message="tns:add" />
			<wsdl:output message="tns:addResponse" />
		</wsdl:operation>
		<wsdl:operation name="minus">
			<wsdl:input message="tns:minus" />
			<wsdl:output message="tns:minusResponse" />
		</wsdl:operation>
	</wsdl:portType>


	<wsdl:binding name="myServiceSOAP" type="tns:IMyService">
		<soap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="add">
			<wsdl:input>
				<soap:body use="literal" />
				<!-- head -->
				<soap:header use="literal" part="licenseInfo" message="tns:licenseInfo" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
		<wsdl:operation name="minus">
			<wsdl:input>
				<soap:body use="literal" />
			</wsdl:input>
			<wsdl:output>
				<soap:body use="literal" />
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="MyServiceImplService">
		<wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort">
			<soap:address location="http://localhost:8888/ms" />
		</wsdl:port>
	</wsdl:service>
</wsdl:deFinitions>


从新使用wsimport生成Java文件拷贝到客户端。

修改服务端的IMyService接口的add方法

 

/**
	 * 
	 * @param b
	 * @param a
	 * @return returns int
	 */
	@WebMethod
	@WebResult(name = "addResult",targetNamespace = "")
	@RequestWrapper(localName = "add",targetNamespace = "http://www.example.org/mywsdl/",className = "org.example.mywsdl.AddType")
	@ResponseWrapper(localName = "addResponse",className = "org.example.mywsdl.AddResponseType")
	public int add(@WebParam(name = "a",targetNamespace = "") int a,@WebParam(name = "b",targetNamespace = "") int b,@WebParam(name = "licenseInfo",header = true) String licenseInfo);

 

实现类Add方法

@Override
	public int add(int a,int b,String licenseInfo) {
		System.out.println(licenseInfo);
		System.out.println(a + "+" + b + "=" + (a + b));
		return (a + b);
	}


客户端测试方法

public static void main(String[] args)throws Exception {
		
		
		String ns = "http://www.example.org/mywsdl/";
		QName name = new QName(ns,"MyServiceImplService");
		URL url = new URL("http://localhost:8888/ms?wsdl");
		Service service = Service.create(url,name);
		
		QName pname = new QName(ns,"MyServiceImplPort");
		dispatch<SOAPMessage> dis = service.createdispatch(pname,SOAPMessage.class,Service.Mode.MESSAGE);
		
		
		SOAPMessage msg = MessageFactory.newInstance().createMessage();
		SOAPEnvelope enve = msg.getSOAPPart().getEnvelope();
		SOAPHeader header = enve.getHeader();
		SOAPBody body = enve.getBody();
		if(header==null) header = enve.addHeader();
		QName hname = new QName(ns,"licenseInfo","ns");
		header.addHeaderElement(hname).setValue("client headHandler message.......");
		
		QName bname = new QName(ns,"add","ns");
		SOAPBodyElement ele = body.addBodyElement(bname);
		ele.addChildElement("a").setValue("12");
		ele.addChildElement("b").setValue("33");
		
		msg.writeto(System.out);
		System.out.println("\n invoking...");
		
		SOAPMessage rep = dis.invoke(msg);
		
		rep.writeto(System.out);
	}


 

结果,客户端控制台:

服务端控制台:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐