详解使用w​s​g​e​n​和​w​s​i​m​p​o​r​t​开​发​W​e​b​S​e​r​v​i​c​e

本文旨在描述如何使用jdk自带的wsgen.exe 和wsimport.exe开发WebService。

1.新建一个java工程,项目名:WebServiceDemo。由于本文想通过完全手动建立的方式来演示整个过程,故我们的工程目录就是WebServiceDemo啦!

2.在此目录下新建Business.java 和 BusinessImpl.java两个类,分别如下:

public interface Business {
	public String echo(String message);	
}
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;

@WebService(name="Business",serviceName="BusinessService",targetNamespace="http://webservice.chapter1.book/client")
@SOAPBinding(style=SOAPBinding.Style.RPC)
public class BusinessImpl implements Business {

	public String echo(String message) {
		if("quit".equalsIgnoreCase(message.toString())){
			System.out.println("Server will be shutdown!");
			System.exit(0);
		}
		System.out.println("Message from client: "+message);
		return "Server response: "+message;
	}

}
3. cmd到命令行模式下,将此二文件编译为class文件,然后运行如下命令生成wsdl文件,注意之前应该在WebServiceDemo目录下新建一个叫wsdl的文件夹。

运行完上面的命令,我们会发现在wsdl目录下生成了BusinessService.wsdl文件,用notepad打开会发下就是一个xml格式的文件。

4.接下来我们创建一个Server.java的类,用来发布Service。代码如下;

import javax.xml.ws.Endpoint;

public class Server {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Endpoint.publish("http://localhost:9527/BusinessService", new BusinessImpl());
		System.out.println("Server has been started");
	}

}

编译然后运行,在浏览器中输入URL:http://localhost:9527/BusinessService?wsdl 如果出现一个XML文档,就说明发布成功了。


注意浏览器中显示的就是我们在wsdl目录下生成的BusinessService.wsdl文件内容,这个文件就是我们后面的wsimport.exe命令执行时要用到的。

5.在Server运行的条件下,下面我们通过wsimport来生成客户端执行类。另起一个命令行窗口,执行如下命令:

成功执行后,我们会发现在WebServiceDemo路径下生成了如下的4个文件:

注意目录结构就是我们之前在BusinessImpl.java类中的targetNamespace指定的,也即对应wsdl文件中的路径,只是转换为目录结构时要注意域名倒着写。

targetNamespace="http://webservice.chapter1.book/client"
OK,至此我们完成了用w​s​g​e​n​和​w​s​i​m​p​o​r​t​开​发​W​e​b​S​e​r​v​i​c​e的任务。

6.下面就是通过一个客户端Client.java 类来验证webservice的时候了:

import java.io.BufferedReader;
import java.io.InputStreamReader;

import book.chapter1.webservice.client.Business;
import book.chapter1.webservice.client.BusinessService;

public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		BusinessService businessService=new BusinessService();
		Business business=businessService.getBusinessPort();
		BufferedReader systemIn=new BufferedReader(new InputStreamReader(System.in));
                while(true){
			String command=systemIn.readLine();
			if(command==null || "quit".equalsIgnoreCase(command.trim())){
				System.out.println("Client quit!");
				try{
					business.echo(command);
				}
				catch(Exception e){
					// IGNORE
				}
				System.exit(0);
			}
			System.out.println(business.echo(command));
		}
	}

}
编译运行,发送一个消息后看到Server端收到并响应。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
目  录  一 使用Axis和XFire布Web Service ......................................................................................... 2  1.1 准备工作............................................................................................................................ 2  1.1.1 新建工程 ................................................................................................................. 2 1.1.2 axis依赖文件 ........................................................................................................... 3 1.1.3 XFire依赖文件 ........................................................................................................ 3 1.2 Axis布Web Service ........................................................................................................ 4  1.2.1 传递简单数据类型的Web Service ........................................................................ 4 1.2.2 带自定义对象传递的Web Service ........................................................................ 8 1.3 XFire布Web Services ................................................................................................... 10  1.3.1 传递简单数据类型的Web Service ...................................................................... 10 1.3.2传递复杂数据类型的Web Service ....................................................................... 12 附录: 1Server的结构 ............................................................................................................. 16 二 调用Web Services .................................................................................................................... 17  2.1 准备工作.......................................................................................................................... 17  2.1.1 新建工程 ............................................................................................................... 17 2.1.2 启动Tomcat上布的1Server服务 ................................................................... 17 2.2 Axis布的Web Service调用方式 ................................................................................. 18  2.2.1 Dynamic Invocation Interface (DII)方式 ................................................................. 18 2.2.2 Dynamic Proxy方式 ............................................................................................... 19 2.2.3 Stubs方式 .............................................................................................................. 20 2.3调用XFire布的Web Service ....................................................................................... 22  2.3.1服务以jar包形式布的时候 直接调用 ............................................................ 23 2.3.2 使用Axis的Client包 ........................................................................................... 23 2.3.3 使用Xfire的Client包 .......................................................................................... 24 附录2:2Client目录结构 ..................................................................................................... 31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值