您的位置:首页 > 其它

WebService系列博客{四}[基于接口的发布服务和测试客户端]

2013-01-09 09:13 555 查看
1、 编写接口代码如下

import javax.jws.WebService;
@WebService
public interface WsServerDao {
	public int add(int a,int b);
	public int minute(int a,int b);
}


2、 编写实现类如下(注意@WebService的Annotation内部声明的接口)

//声明实现接口的endpoint
@WebService(endpointInterface="com.java.ws.WsServerDao")
public class WsServerImpl implements WsServerDao{

	@Override
	public int add(int a, int b) {
		System.out.println("a+b="+(a+b));
		return a+b;
	}

	@Override
	public int minute(int a, int b) {
		System.out.println("a-b="+(a-b));
		return a-b;
	}
}


3、 发布服务

public class MyServer {
	//发布该webservice
	public static void main(String[] args){
		String address = "http://192.168.1.103:7878/web";
		Endpoint.publish(address,new WsServerImpl());
		System.out.println("服务已经启动!");
	}
	
}


4、
模仿客户端解析服务

public class ClientTest {
	public static void main(String[] args) {
		
		try {
			//声明地址的url
			URL url = new URL("http://192.168.1.103:7878/web?wsdl");
			//声明namespace    arg0:访问地址的namespace  ,arg1:namespace后面的服务name
 			QName qname = new QName("http://ws.java.com/","WsServerImplService");	
 			//声明service
			Service service = Service.create(url, qname);
			//利用service拿到接口对象
			WsServerDao wsd = service.getPort(WsServerDao.class);
			//调用方法
			wsd.add(1, 2);
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
}


上述url中得两个参数分别是下图中得圈红部分。Namespace和服务name



说明:

上述程序只是我们对webservice的一个发布和测试。在client端调用的时候由于我们要声明接口对象。但是在现实中我们不可能给调用者接口对象代码。所以上述方法适合自行测试而已
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐