2.使用JDK开发webService
使用jdk开发webService需要注意:jdk版本必须1.6以及1.6以上!
以下webService的组成部分:
server端和client端,通过服务器端(server)webService发布,使用客户端调用。
说明:开发中也许只做server端也许只做client端,以下只是模拟程序。
开发步骤:
1.开发server端:
1.1编写webService编码:
1.1.1创建一个接口(SEI,webService终端接口,该接口方法供client端调用)
/**SEI及其实现类必须添加@WebService注解*/
@WebService
public interface HelloWS { /**SEI方法添加@WebMethod注解*/
@WebMethod
public String sayHello(String name);
}
1.1.2定义HelloWebService接口的实现类
/**SEI实现类添加@WebService注解*/
@WebService
public class HelloWSImpl implements HelloWS { /**该方法用于暴露出去,目的是让客户端来调用*/
/**
* @param name由客户端传入
* return String由服务器返回给客户端
*/
@Override
public String sayHello(String name) {
return "hello: "+name;
} }
1.2发布webService,将以上定义暴露出去以便供客户端调用
public class RealeaseWS { public static void main(String[] args) {
/**address是发布的地址,只要端口不占用,任意端口*/
/**端口后可以跟工程名等等,可以随便写*/
String address = "http://localhost:8989/WebService_Server";
/**
* 使用由Endpoint来发布,这里有两个参数:
* 参数一为发布地址,参数二为SEI接口的实现类对象
*/
Endpoint.publish(address, new HelloWSImpl());
/**如果发布成功,打印*/
System.out.println("webService发布成功!");
}
}
执行程序:
2.开发client端:
新建WebService_Client工程
2.1借助jdk的wsimort.exe工具生成客户端代码
注意:客户端代码生成到client工程下,跟server端是不一样的工程
2.1.1使用jdk提供的wsimport生成客户端代码
2.1.1.1开始,运行,输入cmd打开控制台窗口;
2.1.1.2定位到客户端src目录(客户端代码要生成在这里):
比如我的src目录为:
D:\developUtil\workspace\WebService_Client\src,
那么就是cd D:\developUtil\workspace\WebService_Client\src
2.1.1.3借助wsimport生成客户端代码
在控制台输入:wsimport -keep http://localhost:8989/WebService_Server?wsdl(该路径为服务端发布的address)
生成后,结构图:
注意:由于我没有创建包,所以包是由eclipse帮我创建的
2.2测试程序
测试之前,先看wsdl文档(注意图中粗体、红色、字体较大部分):
<?xml version="1.0" encoding="UTF-8"?><!-- 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://server.webService.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.webService.com/" name="HelloWSImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://server.webService.com/" schemaLocation="http://localhost:8989/WebService_Server?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="sayHello">
<part name="parameters" element="tns:sayHello"></part>
</message>
<message name="sayHelloResponse">
<part name="parameters" element="tns:sayHelloResponse"></part>
</message>
<portType name="HelloWSImpl">
<operation name="sayHello">
<input wsam:Action="http://server.webService.com/HelloWSImpl/sayHelloRequest" message="tns:sayHello"></input>
<output wsam:Action="http://server.webService.com/HelloWSImpl/sayHelloResponse" message="tns:sayHelloResponse"></output>
</operation>
</portType>
<binding name="HelloWSImplPortBinding" type="tns:HelloWSImpl">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="sayHello">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
<service name="HelloWSImplService">
<port name="HelloWSImplPort" binding="tns:HelloWSImplPortBinding">
<soap:address location="http://localhost:8989/WebService_Server"></soap:address>
</port>
</service>
</definitions>
以上三个特殊的粗体部分,分别是:
<portType name="HelloWSImpl">
<service name="HelloWSImplService">
<port name="HelloWSImplPort"
说明:
HelloWSImpl就是服务端的SEI即接口;
HelloWSImplService就是服务端的SEI接口实现类;
HelloWSImplPort方法通过HelloWSImplService对象调用,返回一个HelloWSImpl代理对象,最后通过代理对象调用服务端暴露的方法。
public class TestWebService { public static void main(String[] args) {
HelloWSImplService factory = new HelloWSImplService();
HelloWSImpl helloWSImpl = factory.getHelloWSImplPort();
String result = helloWSImpl.sayHello("webService");
System.out.println(result);
}
}
2.使用JDK开发webService的更多相关文章
- 使用JDK开发WebService
一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...
- WebService学习总结(三)——使用JDK开发WebService
一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...
- WebService-使用JDK开发WebService
一.使用JDK开发WebService 2.1.开发WebService服务器端 1.定义一个interface,使用@WebService注解标注接口,使用@WebMethod注解标注接口中定义的所 ...
- WebService学习--(三)使用JDK开发WebService
一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...
- 【WebService】使用JDK开发WebService(二)
WebService的开发手段 1.使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) WebService的组成 1.服务器端 2.客户端 使用JDK开发WebService a. ...
- WEBSERVICE之JDK开发webservice
转自:https://www.cnblogs.com/w-essay/p/7357262.html 一.开发工具与环境 1. jdk1.6版本以上(jdk1.6.0_21及以上版本) 2 .eclip ...
- [置顶]
WebService学习总结(3)——使用java JDK开发WebService
一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...
- WebService学习总结(三)——使用JDK开发WebService(转)
一.WebService的开发手段 使用Java开发WebService时可以使用以下两种开发手段 1. 使用JDK开发(1.6及以上版本) 2.使用CXF框架开发(工作中) 二.使用JDK开发Web ...
- WebService学习总结(二)--使用JDK开发WebService
一.WebService的开发方法 使用java的WebService时可以使用一下两种开发手段 使用jdk开发(1.6及以上版本) 使用CXF框架开发(工作中) 二.使用JDK开发WebServic ...
随机推荐
- Activity之间传递参数(三)
------siwuxie095 传递值对象,即自定义的有数据类型的对象 1.首先 new 一个 class:User,用于创建自定义对象,同时右键 Generate 出 Constructor.se ...
- JavaWeb 学习004-增删改查的编写
完成了grade,student模块的 数据库连接部分,还需要不断重复这个过程,熟练掌握JDBC的编写. 在不断编写的过程中,加深理解代码. 下一步 1.biz层面的知识内容 2.还有就是登陆成功后跳 ...
- Java(数组)动手动脑
1>数组作为方法参数 阅读并运行示例PassArray.java,观察并分析程序输出的结果,小结,然后与下页幻灯片所讲的内容进行对照. 源代码: // PassArray.java // Pas ...
- python学习之——安装Beautifulsoup、requests、lxml
安装Beautiful soup: 1.下载安装包,解压到python的安装目录: 2.cmd 进入安装包解压后的存放位置: 3.使用命令:python setup.py build , pyt ...
- .net操作word lib DocX
http://cathalscorner.blogspot.hk/2010/06/cathal-why-did-you-create-docx.html using (DocX document = ...
- redis 数据类型
上一篇文章主要写了redis在linux下的安装,这里讲一下redis基本的数据类型,linux的数据类型比较丰富,主要有五种数据类型 .String 字符串类型 常用命令: 除了get.set.in ...
- Codeforces 703B (模拟) Mishka and trip
题目:这里 题意:n个城市,每个城市有个魅力值vi,首先,有n条路将这n个城市连成一个环,1号城市连2号城市,2号连3号****n号连1号城市,每条路的魅力值是其连接的两个城市 的魅力值的乘积,这n个 ...
- UNIX 系统调用函数errno返回值搜集(in updating )
当Unix系统级函数遇到错误时,它们会典型地返回-1,并设置全局整数变量errno来表示什么出错了 阅读redis源码的时候,发现如果对系统级函数出错时的errno比较熟悉,写起程序来会游刃有余不少. ...
- 使用winpcap多线程抓包,以及简单的分析数据包
刚开始使用winpcap数据包的时候,我在抓包的时候使用了 pcap_loop(adhandle, 0, packet_handler, NULL); 这个回调函数进行抓包.同时在回调函数中分析IP地 ...
- Error:SSL peer shut down incorrectly
从别的地方拷贝过来的项目有时会报这个错误,解决方法 File -> Project Structure -> project 对比本地项目和拷贝项目并修改至与本地项目一致