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

Webservice_19_SOAP的基于契约优先WSDL的开发流程

非常感谢孙浩老师。

 

先创建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" />

			<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>

	<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" />
			</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.java到服务器项目中。

实现IMyService的实现类:

package org.example.mywsdl;

import javax.jws.WebService;

@WebService(endpointInterface = "org.example.mywsdl.IMyService",targetNamespace = "http://www.example.org/mywsdl/",wsdlLocation = "meta-inf/wsdl/mywsdl.wsdl")
public class MyServiceImpl implements IMyService {

	public MyServiceImpl() {
	}

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

	@Override
	public int minus(int num1,int num2) {
		System.out.println(num1 + "-" + num2 + "=" + (num1 - num2));
		return (num1 - num2);
	}

}


 

创建服务:

 

package org.example.mywsdl;

import javax.xml.ws.Endpoint;

public class MyService {
	public static void main(String[] args) {
		Endpoint.publish("http://localhost:8888/ms",new MyServiceImpl());
	}
}


 

最终服务端项目的构成:

 

 

在使用wsimport得到客户端Java文件,把所有Java文件拷贝到客户端的项目中。

在运行测试类:

public static void main(String[] args) {
		try {
			URL url = new URL("http://localhost:8888/ms?wsdl");
			QName name = new QName("http://www.example.org/mywsdl/","MyServiceImplService");
			MyServiceImplService implService = new MyServiceImplService(url,name);
			IMyService ms = implService.getMyServiceImplPort();
			System.out.println(ms.minus(33,21));
		} catch (MalformedURLException e) {
			e.printstacktrace();
		}
	}


客户端控制台:

服务端控制台:

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

相关推荐