WebService入门HelloWS

来源:互联网 发布:中国技术贸易数据 编辑:程序博客网 时间:2024/06/09 19:24

编写服务端:


服务端接口:

package com.atguigu.day01_ws.ws;import javax.jws.WebMethod;import javax.jws.WebService;/** * 定义SEI * @author xiao * */@WebServicepublic interface HelloWS {@WebMethodpublic String sayHello(String name);}

服务端实现:

package com.atguigu.day01_ws.ws;import javax.jws.WebService;/** * SEI的实现 * @author xiao * */@WebServicepublic class HelloWSImpl implements HelloWS {@Overridepublic String sayHello(String name) {System.out.println("server sayHello()"+name);return "Hello "+name;}}

服务端的入口main方法:

package com.atguigu.day01_ws.ws.server;import javax.xml.ws.Endpoint;import com.atguigu.day01_ws.ws.HelloWSImpl;/** * 发布webservice * @author xiao * */public class ServerTest {public static void main(String[] args){String address="http://192.168.24.155:8011/day01_ws/hellows";Endpoint.publish(address, new HelloWSImpl());System.out.println("发布Webservice成功!");}}

到此我们的服务端就编写完成:

我们在浏览器中可以查看我们发布的服务:

点击eclipse中的浏览器按钮:


 下面我们利用JDK的工具自动生成一些客户端调用的代码:wsimport.exe

 用命令行在客户端的src目录下输入wsimport -keep url,就可以自动编译服务端,生成一些客户端调用的代码:



此时在客户端会出现如下的包文件:


下面写客户端的调用:

package com.atguigu.day01_ws_clienttest;import com.atguigu.day01_ws.ws.HelloWSImpl;import com.atguigu.day01_ws.ws.HelloWSImplService;/** * 调用webservice * @author xiao * */public class ClientTest {public static void main(String[] args) {HelloWSImplService factory=new HelloWSImplService();HelloWSImpl helloWS=factory.getHelloWSImplPort();System.out.println(helloWS.getClass());String result=helloWS.sayHello("肖红");System.out.println("客户端调用服务端的方法:client "+result);}}
运行之后:



    至此一个由jdk开发的webservice程序就完成了。

     在这个Demo中,我们提到了客户端和服务端,在服务端我们主要编写一个main方法,在这个方法中,我们用到了一个发布地址,在浏览器访问中,我们输入发布地址+?wsdl得到我们发布的服务路径,在客户端我们用JDK工具自动解析wsdl文件,然后在客户端我们就能调用了。






0 0
原创粉丝点击