JAX-WS简单例子

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;

@WebService
public class Hello {
	@WebMethod
	public 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
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值