相关博客:

【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. vue项目中使用vuex

    1.运行 cnpm i vuex -S 2.导入包 import Vuex from 'vuex' 3.注册vuex到vue中 Vue.use(vuex) 4. var store = new Vue ...

  2. WebMagic 启动例子报错

    报错内容: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/Http ...

  3. CentOS 手动配置本地yum源(参考CentOS7 制作 CentOS6本地yum源)

    将原有/etc/yum.repos.d/目录下的文件名全部改为(*.bak),如(红色标记) [root@localhost ~]# cd /etc/yum.repos.d/ [root@localh ...

  4. Co. - Apple - MacBook Pro 快捷键

    Mac 键盘快捷键:https://support.apple.com/zh-cn/HT201236 从windows转到mac的童鞋,可能删除键是心中的一个痛,以前习惯一按delete什么都消失,其 ...

  5. 【ospf-路由聚合】

  6. MySql指令的执行顺序

    1:From 2:On 3:Join 4:Where 5:Group by 5.1:函数 6:Having 7:Select 8:Distinct 9:Order by

  7. PHP判断URL地址百度是否已经收录并主动提交MIP数据

    /** * PHP检测URL地址百度是否已经收录 * @param string $url 要检测的URL地址 */ function Baidu($url) { $url = 'http://www ...

  8. 【Js】Jquery遍历-each(function(e){})中的e和$(this)的区别

    $("selector").each(function(e){ console.log($(e)); console.log($(this)); console.log(e); c ...

  9. PHP入门笔记--基础语法一

    一.基本语法 php标记 <?php ?> php代码结束标记 三种注释 // /**/ # 二.类型 四种标量类型:boolean, integer, float, string 三种复 ...

  10. vm 中 centOS 7 固定ip设置

    虚拟机中,centOS通过NAT连接,设置固定IP上网. 本地主机 VMware Network Adapter VMnet8  状态信息: 描述: VMware Virtual Ethernet A ...