开发手段:
a) 使用JDK开发(1.6及以上版本)
b) 使用CXF框架开发(工作中)
2.组成:
a) 服务器端
b) 客户端
1.开发服务器端
a) Web Service编码:
i. @WebService(SEI和SEI的实现类)
ii. @WebMethod(SEI中的所有方法)
b) 发布Web Service:
i. Endpoint(终端,发布webservice)
2.开发客户端
a) 使用eclipse提供的web service浏览器访问
--查看对应的wsdl文档:...?wsdl(一般浏览器)
--请求webService并查看请求和响应消息(webservice浏览器)
b) 创建客户端应用编码方式访问
--借助jdk的wsimort.exe工具生成客户端代码:
wsimport -keep url //url为wsdl文件的路径
--借助生成的代码编写请求代码
代码片断:
服务端代码:
1.先创建接口
@WebService
public interface HelloWs {
@WebMethod
public String sayHello(String name);
}
2.编写实现类
@WebService
public class HelloWSImpl implements HelloWs {
@Override
public String sayHello(String name) {
System.out.println("server sayhello "+name);
return "hello "+name;
}
}
3.发布webservice
public class ServerTest {
public static void main(String[] args) {
String address = "http://192.168.1.103:8989/day01_ws/hellows";
Endpoint.publish(address, new HelloWSImpl());
System.out.println("发布webservice成功!");
}
}
客户端代码:
1.浏览器访问
2.借助JDK工具生成客户端代码
切换到要生成代码的文件夹内(客户端项目根目录),执行命令wsimport -keep url,生成代码后刷新项目。
红框中即是生成的客户端代码。
3.编写客户端测试类调用webservice代码
public class ClientTest {
public static void main(String[] args) {
HelloWSImplService factory = new HelloWSImplService();
HelloWSImpl helloWS = factory.getHelloWSImplPort();
String result = helloWS.sayHello("xiongdy");
System.out.println("client "+result);
}
}
运行结果client hello xiongdy,表明使用JDK开发webservice成功!