qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

Web service jws自带测试例子

 新建一个服务端项目:
  1、定义一个接口如下:
/**
* 定义webservices接口
* @author Administrator
*
*/
@WebService
public interface IHelloService {
public String sayHello(String name);
public User  getUser(User user);
}
  2.实现接口
/**
* 实现webservices接口
* @author Administrator
*
*/endpointInterface 实现的接口类
@WebService(endpointInterface="com.chni.webservies.IHelloService")
public class HelloServiceImpl implements IHelloService {
public User getUser(User user) {
// TODO Auto-generated method stub
return null;
}
public String sayHello(String name) {
// TODO Auto-generated method stub
return name;
}
}
  3、启动服务(执行下列main函数)
public class TestService {
public static void main(String[] args) {
//服务地址
String address="http://localhost:8989/u";
Endpoint.publish(address, new HelloServiceImpl());
}
}
  4、在浏览器上输入:http://127.0.0.1:8989/u?wsdl 看到以下信息:
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><definitions targetNamespace="http://impl.webservies.chni.com/" name="HelloServiceImplService"><import namespace="http://webservies.chni.com/" location="http://127.0.0.1:8989/u?wsdl=1"/><binding name="HelloServiceImplPortBinding" type="ns1:IHelloService"><soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/><operation name="getUser"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation><operation name="sayHello"><soap:operation soapAction=""/><input><soap:body use="literal"/></input><output><soap:body use="literal"/></output></operation></binding><service name="HelloServiceImplService"><port name="HelloServiceImplPort" binding="tns:HelloServiceImplPortBinding"><soap:address location="http://127.0.0.1:8989/u"/></port></service></definitions>
 5、新建一个客户端项目
  6、在dos命令中使用 如下:d:\ws>wsimport -s . -p com.test.service http://127.0.0.1:8989/u?wsdl 表示意思是:在d盘有个ws文件夹 使用wsimport命令 -s生成客户端源码 -p 指定包名为com.test.service 后面为service 服务地址
  dos界面如下:
  好了你现在去你的d盘ws文件夹下的com这个文件夹考到新建客户端项目的src目录下
  7、新建一个java测试类如下:
public class TestMail {
public static void main(String[] args) {
//建立客户端(HelloServiceImplService 这个往什么地方来的去分析一下wsdl文件中有个service name=“new的这个HelloServiceImplService类”)建议还是去网上看看
IHelloService cilent=new HelloServiceImplService().getHelloServiceImplPort();
String abc=cilent.sayHello("XXX");
System.out.println(abc);
}
}
  输出的值为XXX 说明这个webservice搭建成功了 哈哈

posted on 2014-03-20 11:12 顺其自然EVO 阅读(298) 评论(0)  编辑  收藏 所属分类: 测试学习专栏


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年3月>
2324252627281
2345678
9101112131415
16171819202122
23242526272829
303112345

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜