zoukankan      html  css  js  c++  java
  • 【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也要改变,然后重新发布接口服务,这样是十分不合理的。


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

  • 相关阅读:
    JavaScript 的定时(Timing )事件——setTimeout()与setInterval()
    HTML5+CSS3制作无限滚动与文字跳动效果
    动画属性与过渡属性与渐变属性(全)
    JavaScript 数组2—关联数组
    JavaScript 数组1—索引数组
    什么是JavaScript循环结构?
    JavaScript分支结构Ⅱ—switch-case
    JavaScript分支结构Ⅰ—IF-ELSE
    JavaScript 正则表达式——预定义类,边界,量词,贪婪模式,非贪婪模式,分组,前瞻
    SEO搜索引擎优化是什么?
  • 原文地址:https://www.cnblogs.com/saixing/p/6730228.html
Copyright © 2011-2022 走看看