相关博客:

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

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

【WebService】——契约优先的更多相关文章

  1. webservice07#契约优先#webservice实现简单的动态web项目

    1, 用户管理 User{username,password,nickname} 属性. 2,契约优先[ 先用schema做标准来写wsdl.再生成服务器端的接口,再编写接口的类] 在src下创建目录 ...

  2. webservice系统学习笔记9-使用契约优先的方式的一个服务端demo(隐式传Header信息)

    服务器端: 1.编写wsdl文件 <?xml version="1.0" encoding="UTF-8" standalone="no&quo ...

  3. webservice总结

    webservice xml(DTD,Schema,Stax) SOAP jax-ws (java api xml webservice) 契约优先的开发模式 CXF Rest 异构平台之间的交互(. ...

  4. [WebService]之代码优先方法与契约优先方法

    什么是 web 服务? web 服务是对应用程序功能的网络访问接口,它是使用标准 Internet 技术构建的. 我们目前看到的部署在 Internet 上的 web 服务都是 HTML 网站.其中, ...

  5. cxf构建webservice的两种方式

    一.简介 对于基于soap传输协议的webservice有两种开发模式,代码优先和契约优先的模式.代码优先的模式是通过编写服务器端的代码,使用代码生成wsdl:契约优先模式首先编写wsdl,再通过ws ...

  6. 【WebService】——阶段小结

    [概念] WebService集中解决了远程调用.跨平台和跨语言的问题.如下图中,A应用与B应用之间的相互调用不再局限于平台(Linux或Windows).语言(Java和C#). [与xml] 提到 ...

  7. webservice基础

    一.webservice概念 webservice用于异构平台之间的交互,我用Java写的程序,可以用php..net.pythod等其它语言的程序来访问我的接口.webservice有很多框架帮我们 ...

  8. 开发webservice的方式

        什么是 web 服务? web 服务是对应用程序功能的网络访问接口,它是使用标准 Internet 技术构建的. 我们目前看到的部署在 Internet 上的 web 服务都是 HTML 网站 ...

  9. 重温WCF之数据契约和序列化(四)

    一.数据契约 1.使用数据协定可以灵活控制哪些成员应该被客户端识别. [DataContract] public class Employee { [DataMember] public string ...

随机推荐

  1. HTTP学习之HTTP基础

    学习HTTP技术,首先要了解它的在web通信中有哪些特点,起到什么作用.有哪些规范.都有什么功能. HTTP的特点 HTTP使用的是一种可靠的.快速响应的数据传输协议,用户一旦发起请求,Web服务器可 ...

  2. 分享一个根据具体的日期判断星座的PHP函数

    其实原理很简单,也就是把所有的星座月份日期范围存储到一个数组中,然后根据日期判断属于哪个范围,这样就得到是哪个星座了. 下面的这个函数写的比较精炼,可以参考一下 function constellat ...

  3. JavaScript之DOM查询

    DOM查询 - 通过具体的元素节点来查询 - 元素.getElementsByTagName() - 通过标签名查询当前元素的指定后代元素,返回数组 - 元素.childNodes - 获取当前元素的 ...

  4. python2.7练习小例子(十三)

        13):题目:将一个正整数分解质因数.例如:输入90,打印出90=2*3*3*5.     程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成.(1)如果这个质数恰等于 ...

  5. python2.7入门---循环语句(for&嵌套循环)

        咱们直接先来看for循环.Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串.然后再来看一下它的语法结构: for iterating_var in sequence: ...

  6. Spring AOP(一)——基础概念

    前文的一些内容更多是针对Spring容器内部的一些特性的描述,接下来一个专题将描述Spring AOP的一些信息,配置细节等等. 介绍 面向切面编程(AOP)是一种新的针对程序结构的思路,它补足了面向 ...

  7. bzoj 一些题目汇总

    2140: 稳定婚姻 /* 求联通分量. */ #include<bits/stdc++.h> using namespace std; typedef long long LL; inl ...

  8. 详解jQuery中 .bind() vs .live() vs .delegate() vs .on() 的区别

    转载自:http://zhuzhichao.com/2013/12/differences-between-jquery-bind-vs-live/ 我见过很多开发者很困惑关于jQuery中的.bin ...

  9. 前端学习webpack

    ### 模块化- 为了保证代码充分解耦,一个大的项目拆分成互相依赖的一个一个的小的模块,最后再通过简单的方式合并在一起- 每一个js文件都可以看成一个单独的模块在node这边(服务器端),提出Comm ...

  10. kettle 遇到 解决Incorrect integer value: '' for column 'id' at row 1 完美解决-费元星

    最近自己在测试一个开源的程序,测试中发现.该程序都添加和更新的时候回出现 Incorrect integer value: '' for column 'id' at row 1类是的错误! 后来我自 ...