JAX-WS简单例子

来源:互联网 发布:淘宝达人登录 编辑:程序博客网 时间:2024/06/05 09:29

1、下载jaxws-ri

到jax-ws官网下载,我下载的是jaxws-ri-2.2.8.zip,解压之后lib目录下就是我们开发会需要的用到的jar包。


2、服务端

2.1. 创建Dynamic Web project

需要新建目录build,wsdl和wssrc,目录结构如下图:


2.2. 写webservice如下:(参考了5天学会jaxws-webservice编程)

import javax.jws.WebMethod;import javax.jws.WebService;@WebServicepublic class Hello {@WebMethodpublic String say(String name) {return ("Hello: " + name);}}
@WebService注释在类上,告诉jax-ws这个类是一个webservice;

@WebMethod注释在public方法上,告诉jax-ws这个方法是一个soap方法;


2.3. 进入项目根目录(如:JaxWsServer1),确保wssrc目录清空

wsgen -cp ./WebContent/WEB-INF/classes -r ./wsdl -s ./wssrc -d ./build -wsdl xxx.Hello

执行结果在wsdl和wssrc中生成了文件,我们后面会用到。

参数解释如下:

-cp <path>                 same as -classpath <path>

-r <directory>             resource destination directory, specify where to place resouce files such as WSDLs

-s <directory>            specify where to place generated source files

-d <directory>            specify where to place generated output files

-wsdl[:protocol]         generate a WSDL file.  

The protocol is optional. Valid protocols are soap1.1 and Xsoap1.2, the default is soap1.1.  

Xsoap1.2 is not standard and can only be used in conjunction with the -extension option


2.4. wssrc目录下生成了jaxws目录,目录下包含Say.java和SayResponse.java

这里要说明的是JAX-WS RI 2.2.8使用了JAXB 2.2,并且大量使用注释方式。


2.5. 部署

2.5.1. web方式

a. web.xml中添加listener

    <listener>        <listener-class>            com.sun.xml.ws.transport.http.servlet.WSServletContextListener        </listener-class>    </listener>

b. 新建WebContent/WEB-INF/sun-jaxws.xml

<?xml version="1.0" encoding="UTF-8"?><endpoints xmlns='http://java.sun.com/xml/ns/jax-ws/ri/runtime'version='2.0'><endpoint name='Hello' implementation='xxx.Hello'url-pattern='/HelloService' /></endpoints>
c. 打包在web容器里运行

2.5.2. 程序方式

编写一个main函数,运行即可

public static void main(String[] args) {Endpoint.publish("http://localhost:8808/HelloService", new Hello());}

测试

http://localhost:8080/Hello/HelloService?wsdl


3、客户端(异步方式调用)

3.1. 新建一个java project

3.2. 本地方式获取wsdl

根目录下建一个binding.xml的文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?><bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" wsdlLocation="wsdl/HelloService.wsdl"    xmlns="http://java.sun.com/xml/ns/jaxws">    <bindings node="wsdl:definitions">        <enableAsyncMapping>true</enableAsyncMapping>    </bindings></bindings>

项目根目录中创建wsdl目录,把Server端的wsdl及xsd文件都手工拷贝到客户端工程的wsdl目录下;

项目根目录中运行wsimport -keep -b binding.xml -d bin -s src wsdl/HelloService.wsdl

HelloService中替换file:/C:/workspace_eclipse_3_7/JaxWsAsyncClient1/wsdl/HelloService.wsdl为http://localhost:8080/Hello/HelloService?wsdl

3.3. 服务器方式获取(推荐)

根目录下建一个binding.xml的文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?><bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"    xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" wsdlLocation="http://localhost:8080/Hello/HelloService?wsdl"    xmlns="http://java.sun.com/xml/ns/jaxws">    <bindings node="wsdl:definitions">        <enableAsyncMapping>true</enableAsyncMapping>    </bindings></bindings>

wsimport -keep -b binding.xml -d bin -s src http://localhost:8080/Hello/HelloService?wsdl

3.4. 客户端访问服务端

a. 等待方式

public class HelloAsyncPollingClient {/** * @param args */public static void main(String[] args) throws Exception {HelloService service = new HelloService();Hello port = service.getHelloPort();Response<SayResponse> sayAsync = port.sayAsync("James");while (!sayAsync.isDone()) {System.out.println("is not down");}try {SayResponse callNameResponse = sayAsync.get();String message = callNameResponse.getReturn();System.out.println(message);} catch (Exception ex) {}}}

b. 回调方式

public class HelloAsyncCallBackClient {// 由于此callBack当请求发出去以后当前的这个connection就会关闭 ,为了达到测试的目的,加入了sleep,让客户// 端程序等待服务器端得返回。// callback类型的client要传入一个javax.xml.ws.AsyncHandler类型的匿名内部类,当soapMessage 到达时,Jax-// ws会调handleResponse这个方法来处理response.public static void main(String[] args) throws Exception {HelloService service = new HelloService();Hello port = service.getHelloPort();port.sayAsync("Mk", new AsyncHandler<SayResponse>() {public void handleResponse(Response<SayResponse> res) {try {SayResponse response = null;response = res.get();String message = response.getReturn();System.out.println(message);} catch (Exception e) {e.printStackTrace();}}});Thread.sleep(2000);}}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 怎么查自己宽带账号密码忘了怎么办 电脑重置路由器密码连不上网怎么办 e盘和f盘没有了怎么办 复制文件过程中自己卡住了怎么办 电脑卡住了怎么办 鼠标也点不动 善领wifi连上网不能用怎么办 苹果手机软件更新后网速太慢怎么办 装了固态硬盘开机还慢怎么办 华为荣耀8青春版玩游戏卡怎么办 光猫自带wifi网络很差怎么办 侠盗猎车手自由城之章卡退怎么办? 夏天带头盔晃眼睛太厉害怎么办 别人把钱误转我卡上我怎么办? 家里预留的的网线太短怎么办 电信路由器坏了网线接口断了怎么办 数据线充手机的接头处断了怎么办 新买的小米手环充不进去电怎么办 绝地求生手游被队友故意炸死怎么办 一加3t屏幕开了怎么办? 孕妇吃了8个杏怎么办啊 洗碗盆落水器垫子密封不好怎么办? 手剥橙子剥的特别疼怎么办? 经常带对讲机的耳麦耳朵痛怎么办 公安检查遇到穿便装的军人怎么办 cf幽灵鬼呼吸辅军训刘海怎么办助 眼睛被等离子切割器的光烤了怎么办 玩王者的时候屏幕竖着了该怎么办 铝合金门双包门套比墙的厚了怎么办 磁共振检查后发现带金属了怎么办 贴了乳贴过免痒怎么办 yy别人听见我打游戏的声音怎么办 微信的聊天记录发错邮箱怎么办 百度云盘文件有违规内容怎么办 天籁一键启动钥匙没电怎么办 中兴手机系统界面已停止运行怎么办 怎么办可以复制成不关联的文档 希捷400g硬盘电机不转怎么办 金立手机微信语音播放失败怎么办 手机4g网络变2g怎么办 生存战争2吃了腐烂的食物后怎么办 古筝调音 d的显示为b怎么办