java webservice小例子

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();
  }
 }
}

 

运行此类就可以访问服务了!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值