【WebService】——契约优先

相关博客:

【WebService】——入门实例

【WebService】——SOAP、WSDL和UDDI


前言:

我们先来看一个契约优先的开发实例,通过熟悉他的开发流程,最后再和代码优先的方式进行比较。

Demo中提供了两个方法add()和minus().


1、编写wsdl文件

      在新建的META-INF文件下新建名称为mywsdl的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/">
		<!-- 1.1 编写元素add,addResponse,minus,minusResponse -->
			<xsd:element name="add" type="tns:add"></xsd:element>
			<xsd:element name="addResponse" type="tns:addResponse"></xsd:element>

			<xsd:element name="minus" type="tns:minus"></xsd:element>
			<xsd:element name="minusResponse" type="tns:minusResponse"></xsd:element>
			<!-- 1.2 编写元素的类型 -->
			<xsd:complexType name="add">
				<xsd:sequence>
					<xsd:element name="a" type="xsd:int"></xsd:element>
					<xsd:element name="b" type="xsd:int"></xsd:element>
				</xsd:sequence>
			</xsd:complexType>

			<xsd:complexType name="addResponse">
				<xsd:sequence>
					<xsd:element name="addResult" type="xsd:int"></xsd:element>
				</xsd:sequence>
			</xsd:complexType>


			<xsd:complexType name="minus">
				<xsd:sequence>
					<xsd:element name="c" type="xsd:int"></xsd:element>
					<xsd:element name="d" type="xsd:int"></xsd:element>
				</xsd:sequence>
			</xsd:complexType>

			<xsd:complexType name="minusResponse">
				<xsd:sequence>
					<xsd:element name="minusResult" type="xsd:int"></xsd:element>
				</xsd:sequence>
			</xsd:complexType>
		</xsd:schema>
	</wsdl:types>
	<!-- 1.3 编写message -->
	<wsdl:message name="add">
		<wsdl:part name="add" element="tns:add"></wsdl:part>
	</wsdl:message>

	<wsdl:message name="addResponse">
		<wsdl:part name="addResponse" element="tns:addResponse"></wsdl:part>
	</wsdl:message>

	<wsdl:message name="minus">
		<wsdl:part name="minus" element="tns:minus"></wsdl:part>
	</wsdl:message>

	<wsdl:message name="minusResponse">
		<wsdl:part name="minusResponse" element="tns:minusResponse"></wsdl:part>
	</wsdl:message>

	<!--1.4 编写port,其中operation为方法 -->
	<wsdl:portType name="IMyService">
		<wsdl:operation name="add">
			<wsdl:input message="tns:add"></wsdl:input>
			<wsdl:output message="tns:addResponse"></wsdl:output>
		</wsdl:operation>

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

<!--1.5 编写binding,其中document类型为默认 -->
	<wsdl:binding name="myServiceSOAP" type="tns:IMyService">
		<soap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http" />
		<wsdl:operation name="add">
			<!-- <soap:operation soapAction="http://www.example.org/mywsdl/NewOperation"/> -->
			<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>

	<!-- 1.6 编写service -->
	<wsdl:service name="MyServiceImplService"><!-- 与公布接口的name一致 -->
		<wsdl:port binding="tns:myServiceSOAP" name="MyServiceImplPort">
			<soap:address location="http://localhost:8989/ms" /><!-- 
				发布地址 -->
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

2、生成代码

同样,使用wsimport命令




在F:/WebService/03中找到生成的代码,copy到项目中。


注意:该阶段生成的代码是依据的是wsdl文件,例如:生成IMyService类中,有add()和minus()方法等。


3、发布服务

1)编写实现类

新建MyServiceImpl类,编写add(),minus()的具体实现。

需要注意的是,要制定wsdlLocation的地址,指明mywsdl的位置。

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

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

	public int minus(int c, int d) {
		System.out.println(c-d);
		return c-d;
	}

}

2) 发布服务

注意:http://localhost:8989/ms即wsdl文件中的服务地址。

public class MyServer {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Endpoint.publish("http://localhost:8989/ms", new MyServiceImpl());

	}

}

4、客户端测试

1)生成客户端代码

同样wsimport命令,只是最后不再是mywsdl.wsdl,而是我们服务的地址 http://localhost:8989/ms?wsdl。


2)测试

	public static void main(String[] args) {
		MyServiceImplService service=new MyServiceImplService();
		IMyService ms=service.getMyServiceImplPort();
		System.out.println(ms.add(3,5));

	}


小结:

      WebService中有两种实现方式:代码优先和契约优先。代码优先就是先编写程序代码,再自动生成wsdl文件。而后者正好相反,通过wsdl生成服务端和客户端,有种逆向工程的意思。


      契约优先的方式虽然没有代码优先简单,但他有他的的优点。首先,契约优先与web服务的语言关联性不大,不受语言的限制。其次,如果先编写代码,如果服务编号,wsdl也要改变,然后重新发布接口服务,这样是十分不合理的。


       因此,如果项目小,需求变动不大,可选择代码优先。反之,则推荐契约优先的方式。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值