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 ...
随机推荐
- greendao对SQLite数据库的增删改查操作
利用greendao操作数据库时,都是以对象或者对象的list来进行增删改查的操作,操作的结果都是用一个list来接收的!!! 1.增加一条记录 Stu stu01=new Stu();stu01.s ...
- 查看Windows服务器登录日志
本文以Windows7系统为例:[控制面板]——[管理工具]——[查看事件日志]——[Windows日志]——[安全].此时在视图窗口应该可以看到登录信息了,如果需要知道具体信息那么可以点击某条记录或 ...
- jQuery Mobile 导航栏
jQuery Mobile 导航栏 导航栏由一组水平排列的链接构成,通常位于页眉或页脚内部. 默认地,导航栏中的链接会自动转换为按钮(无需 data-role="button"). ...
- AndroidSQLite多出一个(db.journal文件原因)
今天在Android开发中中将sqlite的数据库创建之后,发现生成的.db文件的旁边 生成了一个大小为0的与数据库文件同名的.db-journal文件,不明白此文件的用途,于是 google了sql ...
- strstr函数的用法
C语言函数 编辑 包含文件:string.h 函数名: strstr 函数原型: extern char *strstr(char *str1, const char *str2); 语法: ...
- MySQL-->基础-->002-->MySQL存储引擎
mysql的存储引擎主要有:MyISAM和InnoDB MyISAM和InnoDB的主要区别:InnoDB支持事务和参照完整性(即为主键约束,数据库的主键和外键类型一定要一致) 存储引擎是针对表而言而 ...
- Eclipse中调试Android技巧
Android eclipse中程序调试 一:断点调试 用eclipse开发android程序的时,跟VS一样是可以断点单步调试的. 步骤如下. 1 设置断点:在编码窗体的左边框上用鼠标双击,或者右键 ...
- sublime插件@sublimelinter安装使用
sublimelinter插件是一款sublime编辑器的代码校验插件,支持多种语言,对于前端来说主要包含css和js校验. 要是用这款插件 1)安装node,然后在全局安装jshint(npm in ...
- JavaScript中关于时间的知识点
1.计算时间差,天数,小时数,余数 var begintime_ms = Date.parse(new Date(begintime.replace(/-/g, "/"))); / ...
- centos7引导项修复
每次装了双系统,都会发现原来的windows引导项不见了,这让我这个windows重度依赖者情何以堪,所以,必须要把我挚爱的windows给找回来. 翻看了一些网上的教程,看来这并不是一个困难的问题. ...