什么是Web Service?

  WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互。

Web Service有什么用处?

  Web Service可以使两个不同语言和平台编写的程序实现交互,可以发布一些服务共他人使用,也可以调用他人发布的一些服务。

Web Service需要了解的知识:

  soap:一种通信协议通过http发送xml格式的数据。

  wsdl:用来描述Web Service的,也就是Web Service的说明书

  uddl:信息注册中心的实现标准规范,同时也包含一组使企业能将自身提供的Web Service注册,以使别的企业能够发现的访问协议的实现标准。

如何发布一个自己的服务(Web Service)

  Web Service发布和调用都有好几种方法,这里只说明其中的一种

代码如下:

 package com.web.service;

 import javax.jws.WebService;
import javax.xml.ws.Endpoint; @WebService
public class PublishService {
//@webService用注解的方式来将此类定义为一个web服务注意要jdk1.6及以上版本才可以使用
public static void main(String[] args) {
//用Endpoint将此类发布为一个服务端点
Endpoint.publish("http://localhost:6789/hi", new PublishService());
System.out.println("ready...");
} public String sayHi(String name){
return "Hello,"+name;
}
//用static或final修饰的方法不会再wsdl中显示
public static String sayHi1(String name){
return "Hello,"+name;
}
public final String sayHi2(String name){
return "Hello,"+name;
}
}

运行成功后打开浏览器访问服务端点:http://localhost:6789/hi?wsdl会显示如下界面说明发布成功:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://service.web.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://service.web.com/" name="PublishServiceService">
<types>
<xsd:schema>
<xsd:import namespace="http://service.web.com/" schemaLocation="http://localhost:6789/hi?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHi">
<part name="parameters" element="tns:sayHi"/>
</message>
<message name="sayHiResponse">
<part name="parameters" element="tns:sayHiResponse"/>
</message>
<portType name="PublishService">
<operation name="sayHi">
<input wsam:Action="http://service.web.com/PublishService/sayHiRequest" message="tns:sayHi"/>
<output wsam:Action="http://service.web.com/PublishService/sayHiResponse" message="tns:sayHiResponse"/>
</operation>
</portType>
<binding name="PublishServicePortBinding" type="tns:PublishService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHi">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="PublishServiceService">
<port name="PublishServicePort" binding="tns:PublishServicePortBinding">
<soap:address location="http://localhost:6789/hi"/>
</port>
</service>
</definitions>

发布成功后,就可以根据服务端点来调用我们的服务

如何调用Web Service:

  通过DOS命令选择盘符方便找到文件,通过DOS命令:wsimport -s . http://localhost:6789/hi?wsdl

  生成客户端类源文件文件如下图所示:

生成成功找到此文件

新建一个新的项目将此文件copy进来

新建一个测试类

参考 http://localhost:6789/hi?wsdl 中

通过服务名创建对象并调用服务端口调用如下:

 package com.web.test;

 import com.web.service.PublishService;
import com.web.service.PublishServiceService; public class ServiceTest {
public static void main(String[] args) {
//创建服务对象并获取端口
PublishService port = new PublishServiceService().getPublishServicePort();
//调用端口中的sayHi()方法
String hi = port.sayHi("xiaoming");
System.out.println(hi);
}
}

这样就实现了在不同的程序中调用Web Service服务

注意:当发布者的服务器关闭的时候调用服务会报错

自己的总结,如有错误之处,还望各位大牛不吝赐教,谢谢。

Web Service随笔的更多相关文章

  1. 微软BI 之SSIS 系列 - 在 SSIS 中使用 Web Service 以及 XML 解析

    开篇介绍 Web Service 的用途非常广几乎无处不在,像各大门户网站上的天气预报使用到的第三方 Web Service API,像手机客户端和服务器端的交互等都可以通过事先设计好的 Web Se ...

  2. 深入学习Web Service系列----异步开发模式

    概述 在本篇随笔中,通过一些简单的示例来说一下Web Service中的异步调用模式.调用Web Service方法有两种方式,同步调用和异步调用.同步调用是程序继续执行前等候调用的完成,而异步调用在 ...

  3. .NET基础拾遗(7)Web Service的开发与应用基础

    Index : (1)类型语法.内存管理和垃圾回收基础 (2)面向对象的实现和异常的处理 (3)字符串.集合与流 (4)委托.事件.反射与特性 (5)多线程开发基础 (6)ADO.NET与数据库开发基 ...

  4. Web Service概念梳理

    计算机技术难理解的很多,Web Service 对我来说就是一个很难理解的概念:为了弄清它到底是什么,我花费了两周的时间,总算有了一些收获,参考了不少网上的资料,但有些概念说法不一.我以w3c和 一些 ...

  5. 应用Apache Axis进行Web Service开发

    转自(http://tscjsj.blog.51cto.com/412451/84813) 一.概述 SOAP原意为Simple Object Access Protocol(简单对象访问协议),是一 ...

  6. Web Service

    Web Service全称XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求,轻量级的独立的通讯技术.是:通过SOAP ...

  7. WCF、Web API、WCF REST、Web Service比较

    原文地址:http://www.dotnet-tricks.com/Tutorial/webapi/JI2X050413-Difference-between-WCF-and-Web-API-and- ...

  8. Web Service和WCF的区别。其实二者不属于一个范畴!!!

    Web Service和WCF的区别 [1]Web Service:严格来说是行业标准,也就是Web Service 规范. 它有一套完成的规范体系标准,而且在持续不断的更新完善中. 它使用XML扩展 ...

  9. Web Service简要概念,学习记录!

    Web Service平台需要一套协议来实现分布式应用程序的创建.任何平台都有它的数据表示方法和类型系统.要实现互操作性,Web Service平台必须提供一套标准的类型系统,用于沟通不同平台.编程语 ...

随机推荐

  1. springmvc SSM 多数据源 shiro redis 后台框架 整合

    A集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单 下载地址    ; freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类 ...

  2. 张小龙宣布微信小程序1月9日发布,并回答了大家最关心的8个问题

    2016 年 12 月 28 日,张小龙在微信公开课 PRO 版的会场上,宣布了微信小程序的正式发布时间. 微信小程序将于 2017 年 1 月 9 号正式上线. 同时他解释称,小程序就像PC时代的网 ...

  3. Android中点击事件的实现方式

    在之前博文中多次使用了点击事件的处理实现,有朋友就问了,发现了很多按钮的点击实现,但有很多博文中使用的实现方式有都不一样,到底是怎么回事.今天我们就汇总一下点击事件的实现方式. 点击事件的实现大致分为 ...

  4. join Linq

    List<Publisher> Publishers = new List<Publisher>(); Publisher publish1 = new Publisher() ...

  5. sql server 取文件名函数 转载

    /****** Object: UserDefinedFunction [dbo].[GetDirectoryPath] Script Date: 2016-12-16 16:54:05 ****** ...

  6. .NET面试题系列[1] - .NET框架基础知识(1)

    很明显,CLS是CTS的一个子集,而且是最小的子集. - 张子阳 .NET框架基础知识(1) 参考资料: http://www.tracefact.net/CLR-and-Framework/DotN ...

  7. xamarin优化listView.ScrollTo

    在xamarinz中关于listview的滚动,我这里有点小优化,我做了一个类似QQ的聊天页面,上面是一个listview,下面时一个editText,当在手机上使用时,发现在android平台下,如 ...

  8. Java发展历史

    1991年1月 Sun公司成立了Green项目小组,专攻智能家电的嵌入式控制系统 1991年2月 放弃C++,开发新语言,命名为"Oak" 1991年6月 JamesGosling ...

  9. 特殊的数据类型: bit、sql_variant、sysname

    在SQL Server中,特殊的数据类型主要有三个,分别是:bit.sql_variant 和 sysname 一,bit bit类型,只有三个有效值:0,1 和 null,字符串true或false ...

  10. .net中 页面包含子页面 类似include的功能--(记录九)

    aspx页面中: //加上这一段代码 <%@ Register Tagprefix="ImgEvaTask" TagName="content1" src ...