1,myeclipse8.5 ,tomcat6
提供服务端
在myeclipse新建web service project
在src里新建两个包,如下:
接口:Weather.java
public interface Weather {
public void weatherToday();
}
实现类:WeatherImpl.java
public class WeatherImpl implements Weather{
public void weatherToday() {
// TODO Auto-generated method stub
System.out.println("晴朗!!!");
}
}
在services.xml在添加如下内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>Weather</name>
<namespace>www.xx.com/Weather</namespace><!--网站域名+上面节点名 -->
<serviceClass>com.company.xx.inter.Weather</serviceClass>
<implementationClass>com.company.xx.impl.WeatherImpl</implementationClass>
</service>
</beans>
每个节点的含义
- service
service 标签和它所包含的 xml 内容为发布成 Web 服务的 POJO 提供完整的描述。
- name
Web 服务被发布时所采用的唯一名称。
- namespace
Web 服务发布时所使用的命名空间。
- serviceClass
Web 服务接口类的全名,包括包名和类名。
- implemetationClass
Web 服务实现类的全名,包括包名和类名。
然后启动tomcat,在浏览器中输入http://localhost:8080/myWebService/services/Weather?wsdl。出现一个xml文件就说明发布成功。
其目录结构如:
其中Weather.java 必须和提供方是同一个接口
ServiceTest.java内容:
public class ServiceTest {
public static void main(String[] args) {
Service service = new ObjectServiceFactory().create(Weather.class);
XFire xFire = XFireFactory.newInstance().getXFire();
XFireProxyFactory factory = new XFireProxyFactory(xFire);
String url = "http://localhost:8080/myWebService/services/Weather";
try {
Weather ww = (Weather)factory.create(service, url);
ww.weatherToday();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
运行此类就可以访问服务了!!!