接口开发与调用(一)
1.接口的开发(wsdl)
自定义一个接口
package test.impltest;
import javax.jws.WebService;
//定义一个接口
@WebService
public interface dognumber {
String queryWeather();
}
定义一个实现类,实现该接口
package test.impltest;
import javax.jws.WebService;
//定义一个实现类,实现该接口
@WebService
public class dognumbersimpl implements dognumber{
@Override
public String queryWeather() {
// TODO Auto-generated method stub
return "20条狗";
}
}
定义WebService的发布地址,通过EndPoint(端点服务)发布一个WebService
package test.impltest;
import javax.xml.ws.Endpoint;
public class MyListener {
public static void main(String[] args){
//定义WebService的发布地址,这个地址就是提供给外界访问Webervice的URL地址,URL地址格式为:http://ip:端口号/xxxx
String address="http://localhost:8080/dognumber";
//使用Endpoint类提供的publish方法发布WebService,发布时要保证使用的端口号没有被其他应用程序占用
Endpoint.publish(address, new dognumbersimpl());
System.out.print("发布成功");
}
}

发布成功后 在浏览器输入 http://localhost:8080/dognumber?wsdl

2.接口的调用
下载客户端代码




然后点击Finish。
创建一个类,写一个main方法

输出如下,表示调用成功

注意:如果webservice用到的端口最好与tomcat的访问端口不一样,否则,会出现无法访问项目。
网友评论