服务器端开发
package com.ws.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWS {
@WebMethod
String sayHello(String name);
}
- 实现类也要加上@WebService注解
package com.ws.service;
import javax.jws.WebService;
@WebService
public class HelloWSImpl implements HelloWS {
@Override
public String sayHello(String name) {
return "hello "+name;
}
}
package com.ws.main;
import javax.xml.ws.Endpoint;
import com.ws.service.HelloWSImpl;
public class Main {
public static void main(String[] args) {
//必须写本地IP
String address = "http://172.17.202.155:9999/ws01/hellows";
Endpoint.publish(address,new HelloWSImpl());
System.out.println("发布成功");
}
}
运行main之后,可以看到上图箭头的位置并没有变灰,说明程序还在运行,它正在等待被访问
用浏览器查看wsdl文档,路径是
http://172.17.202.155:9999/ws01/hellows?wsdl
在eclipse工具栏有一个webservice浏览器,我们使用它来测试一下
打开webservice浏览器后,按下图步骤,输入url,点击go
然后可以测试我们发布的接口了
客户端开发
- 新建一个java工程作为客户端
打开一个cmd命令行
在命令行中进入客户端项目的src所在目录,我们需要在这里使用命令生成客户端代码,命令是:wsimport -keep url。url是我们发布的webservice的url加上
?wsdl
,回车,自动生成代码
package com.ws.service.client.test;
import com.ws.service.HelloWSImpl;
import com.ws.service.HelloWSImplService;
public class Main {
public static void main(String[] args) {
// 调用webservice
HelloWSImplService factory = new HelloWSImplService();
HelloWSImpl helloWS = factory.getHelloWSImplPort();
System.out.println(helloWS.sayHello("tom"));
}
}
客户端开发完毕
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。