什么是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发布和调用都有好几种方法,这里只说明其中的一种

代码如下:

 1 package com.web.service;
 2
 3 import javax.jws.WebService;
 4 import javax.xml.ws.Endpoint;
 5
 6 @WebService
 7 public class PublishService {
 8     //@webService用注解的方式来将此类定义为一个web服务注意要jdk1.6及以上版本才可以使用
 9     public static void main(String[] args) {
10         //用Endpoint将此类发布为一个服务端点
11         Endpoint.publish("http://localhost:6789/hi", new PublishService());
12         System.out.println("ready...");
13     }
14
15     public String sayHi(String name){
16         return "Hello,"+name;
17     }
18     //用static或final修饰的方法不会再wsdl中显示
19     public static String sayHi1(String name){
20         return "Hello,"+name;
21     }
22     public final String sayHi2(String name){
23         return "Hello,"+name;
24     }
25 }

运行成功后打开浏览器访问服务端点: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 中

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

 1 package com.web.test;
 2
 3 import com.web.service.PublishService;
 4 import com.web.service.PublishServiceService;
 5
 6 public class ServiceTest {
 7     public static void main(String[] args) {
 8         //创建服务对象并获取端口
 9         PublishService port = new PublishServiceService().getPublishServicePort();
10         //调用端口中的sayHi()方法
11         String hi = port.sayHi("xiaoming");
12         System.out.println(hi);
13     }
14 }

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

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

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

转载于:https://www.cnblogs.com/hcl22/p/6189399.html

Web Service随笔相关推荐

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

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

  2. Reporting Services 4: Web Service

    Reporting Services提供了一个基于Web的报表管理器Report Manager,其主要功能是通过是调用报表服务器提供的Web Service来完成的,可以使用类似于http://(S ...

  3. Web Service 安全性解决方案(SOAP篇)

    拼吾爱程序人生 » 软件编程 » Visual Studio.NET » Web Service » Web Service 安全性解决方案(SOAP篇) Web Service 安全性解决方案(SO ...

  4. Web service到底是什么?

    Web service到底是什么:在什么情况下你应该使用Web service. 分布式应用程序和浏览器 研究一下当前的应用程序开发,你会发现一个绝对的倾向:人们开始偏爱基于浏览器的瘦客户应用程序.这 ...

  5. web service 和 remoting 有什么区别

    其实现的原理并没有本质的区别,在应用开发层面上有以下区别: 1.Remoting可以灵活的定义其所基于的协议,如果定义为HTTP,则与Web Service就没有什么区别了,一般都喜欢定义为TCP,这 ...

  6. 使用axis开发web service服务端

    一.axis环境搭建 1.安装环境 JDK.Tomcat或Resin.eclipse等. 2.到 http://www.apache.org/dyn/closer.cgi/ws/axis/1_4 下载 ...

  7. WCF实现RESTFul Web Service

    共同学习了前面一些概念,终于开始正题了哈.RESTful的Web Service调用直观,返回的内容容易解析.这里先会描述一个简单的场景--Web Service提供一个方法来搜索个人信息,传入人名, ...

  8. web service 项目 和 普通 web项目 的 区别

    web service 面向的是开发者(需要再次开发) 普通web 面向的是用户(直接使用) 转载于:https://www.cnblogs.com/zno2/p/5612024.html

  9. 从WEB SERVICE 上返回大数据量的DATASET

    前段时间在做一个项目的时候,遇到了要通过WEB SERVICE从服务器上返回数据量比较大的DATASET,当然,除了显示在页面上以外,有可能还要用这些数据在客户端进行其它操作.查遍了网站的文章,问了一 ...

最新文章

  1. 猎豹MFC--TH_双管道--对管道的操作就想对文件的操作一样
  2. matlab条形图添加误差线_在Excel图表中添加误差线和对误差线进行设置的技巧
  3. application.properties amp;amp;amp; application.yml 配置文件详解(转)
  4. asterisk1.8 for mipsel mysql
  5. 解决:Whitelabel Error Page This application has no explicit mapping for /error...UnknownHostException
  6. MongoDB索引案例一则
  7. n皇后问的三种解答方式
  8. linux scp 输入密码,Linux scp远程复制数据不需要输入密码
  9. iOS开发之touchesCancelled
  10. VBA实现数据库中的字段处理(下划线去掉,后面的字母变大写)之版本1.1。
  11. matlab2c使用c++实现matlab函数系列教程-histc函数
  12. 掷骰子python代码_掷骰子游戏,,游戏规则:玩家投掷两个骰
  13. 图的遍历:BFS算法学习
  14. CSS之transform的translate平移属性【2D】(一)
  15. 爬虫代理哪家强?十大付费代理详细对比评测!
  16. OpenHarmony鸿蒙相关资料
  17. WebView 正确设置cookie 的方法
  18. android开机字库加载过程,小米手机字库维修更换和EMMC字库编程烧写方法教程
  19. 怎么快速无损地把avi转换mov格式?简单3步就能搞定!
  20. webaudio_WebAudio Deep Note,第5部分:增益节点

热门文章

  1. proteus数码管不亮是什么原因_人行道闸开后不关的原因是什么?速来get一下
  2. android studio grandle错误,flutter android studio构建失败
  3. [设计模式-行为型]备忘录模式(Memento)
  4. 外星人电脑为什么那么贵_为什么百丽的鞋那么贵
  5. 单片机c语言数码显示实验报告,单片机c语言版数码动态显示实验报告.doc
  6. lua怎么嵌入php,linux下安装php的lua扩展
  7. 笔记本html外接显示器,笔记本如何外接显示器
  8. 华为哪款手表支持鸿蒙,华为Watch 3最早或于5月发布 采用鸿蒙系统并支持eSIM
  9. ice通信原理_变频开关电源工作原理,开关电源自我检修
  10. 线程池ThreadPoolExcutor的使用