WebService的开发手段

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

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

WebService的组成

  1、服务器端

  2、客户端

使用JDK开发WebService

  a、开发WebService服务端

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

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

 package com.test.ws;

 import javax.jws.WebMethod;
import javax.jws.WebService; /**
* 定义SEI(WebService EndPoint Interface)终端
* @author H__D
* @date 2017年7月28日 上午11:35:34
*
*/
//使用@WebService注解标注WebServiceI接口
@WebService
public interface HelloWS { //使用@WebMethod注解标注WebServiceI接口中的方法
@WebMethod
public String sayHello(String name); }

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

 package com.test.ws;

 import javax.jws.WebService;

 /**
* SEI的具体实现
* @author H__D
* @date 2017年7月28日 上午11:37:43
*
*/
//使用@WebService注解标注
@WebService
public class HelloWSImpl implements HelloWS{ @Override
public String sayHello(String name) {
System.out.println("WebService sayHello : " + name);
return "Hello : " + name;
}
}

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

 package com.test.ws.server;

 import javax.xml.ws.Endpoint;

 import com.test.ws.HelloWSImpl;

 /**
* 发布Web Service
* @author H__D
* @date 2017年7月28日 上午11:40:48
*
*/
public class ServerTest { public static void main(String[] args) { //定义WebService的发布地址,这个地址就是提供给外界访问Webervice的URL地址,URL地址格式为:http://ip:端口号/xxxx
String address = "http://127.0.0.1:8989/test-webservice/hellows";
//使用Endpoint类提供的publish方法发布WebService,发布时要保证使用的端口号没有被其他应用程序占用
Endpoint.publish(address, new HelloWSImpl());
System.out.println("发布webservice成功!"); }
}

    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如下:
      
      然后在请求框中查看源,同理在响应框中查看源,可以看到发送的请求消息和响应消息
      
      请求消息

 <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">
<soapenv:Body>
<q0:sayHello>
<arg0>tom</arg0>
</q0:sayHello>
</soapenv:Body>
</soapenv:Envelope>

      响应消息

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:sayHelloResponse xmlns:ns2="http://ws.test.com/">
<return>Hello : tom</return>
</ns2:sayHelloResponse>
</S:Body>
</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接口的使用即可,如下:

 package com.test.ws.client;

 import com.test.ws.HelloWSImpl;
import com.test.ws.HelloWSImplService; /**
* 调用WebService的客户端
* @author H__D
* @date 2017年7月28日 下午2:39:24
*
*/
public class WSClient { public static void main(String[] args) {
//创建一个用于产生WebServiceImpl实例的工厂,WebServiceImplService类是wsimport工具生成的
HelloWSImplService factory = new HelloWSImplService();
//通过工厂生成一个WebServiceImpl实例,WebServiceImpl是wsimport工具生成的
HelloWSImpl helloWS = factory.getHelloWSImplPort();
System.out.println(helloWS.getClass()); //调用WebService的sayHello方法
String result = helloWS.sayHello("Jack");
System.out.println(result);
}
}

    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. PHPActiveRecord 学习一

    #连接数据库 <?phprequire_once dirname(__FILE__) . '/../../ActiveRecord.php'; // initialize ActiveRecor ...

  2. oracle 存储过程模板

    CREATE OR REPLACEPROCEDURE PROCE_NAME(V_IN varchar2, V_OUT out varchar2) ASBEGIN --...  commit;  V_O ...

  3. spring xml头文件xmlns和xsi的意思

    在spring的配置中,总能看见如下的代码: <beans xmlns="http://www.springframework.org/schema/beans" xmlns ...

  4. oracle连接错误:ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务解决

    自己的解决办法是,把数据库连接字符串的默认SID_NAME = ORCL改为 sid_name =test(自己安装数据库时候改的名字).即可正常连接. 网上搜罗的其他问题:把监听服务重启下.(自己的 ...

  5. 常用的jquerymobil 站点

    http://www.jqmapi.com/api1.2/ Jquery Mobile 中文API站 https://codiqa.com/demo   jquerymobil UI编辑器 https ...

  6. 利用MATLAB截取一张复杂图片中想要的区域

    A = imread('1.jpg'); imshow(A); [x,y] = ginput(2);    %确定图像上的两点利用ginput函数,返回值是两点的坐标 pic_1 = imcrop(A ...

  7. 第二章 向量(a)接口与实现

  8. CardView 卡片布局

    转自:https://www.baidu.com/link?url=WwHvfX3PB_egfQ6GFwxsDeq4NDzB2AW-zaTzskkNXs0qWnIcHyh3pN3Oqe6YO1lAmV ...

  9. Android Studio生成签名安装包(Generate Signed APK)

    一 打开构建对话框. 二 创建新的密钥库(key store) 可以选择已创建的密钥库,也可以选择创建新的密钥库. 创建完成后,自动导入. 三 选择签名类型. 如果不选,会提示错误. 这里将新旧两种签 ...

  10. [LeetCode_96] Unique Binary Search Trees

    题目链接 https://leetcode.com/problems/unique-binary-search-trees/ 题意 计算给定节点数的BST有多少种 思路 递归 相关知识 二叉搜索树(B ...