WebService的开发手段

  1、使用JDK开发(1.6及以上版本)

  2、使用CXF框架开发(工作中)

WebService的组成

  1、服务器端

  2、客户端

使用JDK开发WebService

  a、开发WebService服务端

    1、使用eclipse新建一个服务端java工程
    

    2、新建一个接口,使用@WebService(SEI和SEI的实现类)注解标注接口,使用@WebMethod(SEI中的所有方法)注解标注接口中定义的所有方法,如下:      

  1. package com.test.ws;
  2.  
  3. import javax.jws.WebMethod;
  4. import javax.jws.WebService;
  5.  
  6. /**
  7. * 定义SEI(WebService EndPoint Interface)终端
  8. * @author H__D
  9. * @date 2017年7月28日 上午11:35:34
  10. *
  11. */
  12. //使用@WebService注解标注WebServiceI接口
  13. @WebService
  14. public interface HelloWS {
  15.  
  16. //使用@WebMethod注解标注WebServiceI接口中的方法
  17. @WebMethod
  18. public String sayHello(String name);
  19.  
  20. }

    3、编写一个接口实现类,使用@WebService注解标注实现类,如下:

  1. package com.test.ws;
  2.  
  3. import javax.jws.WebService;
  4.  
  5. /**
  6. * SEI的具体实现
  7. * @author H__D
  8. * @date 2017年7月28日 上午11:37:43
  9. *
  10. */
  11. //使用@WebService注解标注
  12. @WebService
  13. public class HelloWSImpl implements HelloWS{
  14.  
  15. @Override
  16. public String sayHello(String name) {
  17. System.out.println("WebService sayHello : " + name);
  18. return "Hello : " + name;
  19. }
  20. }

    4、使用Endpoint(终端)类发布webservice,如下:

  1. package com.test.ws.server;
  2.  
  3. import javax.xml.ws.Endpoint;
  4.  
  5. import com.test.ws.HelloWSImpl;
  6.  
  7. /**
  8. * 发布Web Service
  9. * @author H__D
  10. * @date 2017年7月28日 上午11:40:48
  11. *
  12. */
  13. public class ServerTest {
  14.  
  15. public static void main(String[] args) {
  16.  
  17. //定义WebService的发布地址,这个地址就是提供给外界访问Webervice的URL地址,URL地址格式为:http://ip:端口号/xxxx
  18. String address = "http://127.0.0.1:8989/test-webservice/hellows";
  19. //使用Endpoint类提供的publish方法发布WebService,发布时要保证使用的端口号没有被其他应用程序占用
  20. Endpoint.publish(address, new HelloWSImpl());
  21. System.out.println("发布webservice成功!");
  22.  
  23. }
  24. }

    5、运行SeverTest类的main方法,使用浏览器进行访问,访问地址:http://127.0.0.1:8989/test-webservice/hellows,如下:
      控制他输出:
      
      网页访问:
      

    6、使用Eclipse的Web Services Explorer,测试访问WebService,其中要输入wsdl地址(一般是发布的WebService的Endpoint后面加上'?wsdl'):
      
      
      

    7、查看发送消息和响应消息的具体内容,在Eclipse的Web Services Explorer模块中的Status中,点击Source如下:
      
      然后在请求框中查看源,同理在响应框中查看源,可以看到发送的请求消息和响应消息
      
      请求消息

  1. <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:q0="http://ws.test.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  2. <soapenv:Body>
  3. <q0:sayHello>
  4. <arg0>tom</arg0>
  5. </q0:sayHello>
  6. </soapenv:Body>
  7. </soapenv:Envelope>

      响应消息

  1. <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  2. <S:Body>
  3. <ns2:sayHelloResponse xmlns:ns2="http://ws.test.com/">
  4. <return>Hello : tom</return>
  5. </ns2:sayHelloResponse>
  6. </S:Body>
  7. </S:Envelope>

  b、开发WebService客户端

    1、使用eclipse新建一个客服端java工程
      

    2、使用jdk的wsimort.exe工具生成客户端代码,执行命令:wsimport -keep url(url为wsdl文件的路径)生成客户端代码,wsimort.exe工具位于jdk的bin目录下,如下:
      
      打开命令行窗口,切换到src目录中,执行命令:wsimport -keep http://127.0.0.1:8989/test-webservice/hellows?wsdl,如下:
      
      刷新工程,可以看到代码已经生成,如下
      

    3、编写调用WebService对外提供的方法
      wsimport工具帮我们生成了好几个java类,但我们只需要关心WebServiceImplService类和WebServiceImpl接口的使用即可,如下:

  1. package com.test.ws.client;
  2.  
  3. import com.test.ws.HelloWSImpl;
  4. import com.test.ws.HelloWSImplService;
  5.  
  6. /**
  7. * 调用WebService的客户端
  8. * @author H__D
  9. * @date 2017年7月28日 下午2:39:24
  10. *
  11. */
  12. public class WSClient {
  13.  
  14. public static void main(String[] args) {
  15. //创建一个用于产生WebServiceImpl实例的工厂,WebServiceImplService类是wsimport工具生成的
  16. HelloWSImplService factory = new HelloWSImplService();
  17. //通过工厂生成一个WebServiceImpl实例,WebServiceImpl是wsimport工具生成的
  18. HelloWSImpl helloWS = factory.getHelloWSImplPort();
  19. System.out.println(helloWS.getClass());
  20.  
  21. //调用WebService的sayHello方法
  22. String result = helloWS.sayHello("Jack");
  23. System.out.println(result);
  24. }
  25. }

    4、运行main方法,控制台输出,可以看到已经成功调用了WebService,如下:
      

    

【WebService】使用JDK开发WebService(二)的更多相关文章

  1. WEBSERVICE之JDK开发webservice

    转自:https://www.cnblogs.com/w-essay/p/7357262.html 一.开发工具与环境 1. jdk1.6版本以上(jdk1.6.0_21及以上版本) 2 .eclip ...

  2. WebService学习总结(二)--使用JDK开发WebService

    一.WebService的开发方法 使用java的WebService时可以使用一下两种开发手段 使用jdk开发(1.6及以上版本) 使用CXF框架开发(工作中) 二.使用JDK开发WebServic ...

  3. 使用JDK开发WebService

    一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...

  4. WebService学习总结(三)——使用JDK开发WebService

    一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...

  5. WebService学习--(三)使用JDK开发WebService

    一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...

  6. [置顶] WebService学习总结(3)——使用java JDK开发WebService

    一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...

  7. WebService学习总结(三)——使用JDK开发WebService(转)

    一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...

  8. 2.使用JDK开发webService

    使用jdk开发webService需要注意:jdk版本必须1.6以及1.6以上! 以下webService的组成部分: server端和client端,通过服务器端(server)webService ...

  9. WebService-使用JDK开发WebService

    一.使用JDK开发WebService 2.1.开发WebService服务器端 1.定义一个interface,使用@WebService注解标注接口,使用@WebMethod注解标注接口中定义的所 ...

随机推荐

  1. cdh5.13.1 hadoop hdfs HA模式无法启动

    经过观察日志发现,JN三个节点启动正常,只有NN节点启动时提示JN节点没有格式化 停止HDFS下面所有服务 先启动JN节点 然后启动一个NN节点,观察三个JN节点日志 发现其中一个节点的日志正常,没有 ...

  2. 全国高校绿色计算大赛 预赛第一阶段(C++)第4关:计算日期

    挑战任务 我们吃的食物都有保质期,现在食品监督管理局想要制作一个能准确计算食品过期日期的小程序,需要请你来进行设计. 例如:A食品在2018年1月1日生产,保质期是20天,则它的过期日期在2018年1 ...

  3. NumPy 切片和索引

    NumPy 切片和索引 ndarray对象的内容可以通过索引或切片来访问和修改,与 Python 中 list 的切片操作一样. ndarray 数组可以基于 0 - n 的下标进行索引,切片对象可以 ...

  4. 中国剩余定理模板 51nod 1079

    题目链接:传送门 推荐博客:https://www.cnblogs.com/freinds/p/6388992.html (证明很好,代码有误). 1079 中国剩余定理  基准时间限制:1 秒 空间 ...

  5. hdu 2089 数位dp

    链接:https://vjudge.net/problem/23625/origin 中文,题目不用说了. 其实这题的数据很小,所以直接暴力也可以过,但是还是要学会数位dp,因为并不是每一题的数据都会 ...

  6. Mac 上fopen总返回NULL

    全局,相对路径都不行, 在沙盒中获取也不行 //在沙盒中获取Documents的完整路径 NSString * path = [NSSearchPathForDirectoriesInDomains( ...

  7. AnguarJS——第10章 路由

    第10章 路由 一个应用是由若个视图组合而成的,根据不同的业务逻辑展示给用户不同的视图,路由则是实现这一功能的关键. 10.1 SPA SPA(Single Page Application)指的是通 ...

  8. DMZ原理与应用

    DMZ是英文“demilitarized zone”的缩写,中文名称为“隔离区”,“非军事化区”.它是为了解决安装防火墙后外部网络不能访问内部网络服务器的问题,而设立的一个非安全系统与安全系统之间的缓 ...

  9. es6问答

    1. 箭头函数的特点 *箭头函数this的指向是定义时所在的对象,而不是使用时所在的对象: * 箭头函数不能做构造函数 * 不能使用argument对象 *不能使用yield命令 2.let cons ...

  10. ios 错误纪录

    .self.出不来的原因 Member reference type 'struct objc_class *' is a pointer; maybe you meant to use '-> ...