web服务

    技术2022-06-27  47

    author:何桂坤

    --------services.xml文件-------------------

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://xfire.codehaus.org/config/1.0"><service>  <name>weatherService</name>  <serviceClass>ch09.server.Iweather</serviceClass>  <implementationClass>ch09.server.WeatherImpl</implementationClass>  <scope>application</scope>

    </service><service>  <name>weekWork</name>  <serviceClass>ch09.server.IweekWork</serviceClass>  <implementationClass>ch09.server.weekWorkImpl</implementationClass>  <scope>application</scope>  </service> <!-- 配置文件解析  <service>        当前服务的名称(为客户端使用)比如要测试次文件时 就以这个名为访问名    <name>CreditCard</name>        指定服务的名称空间有银行(提供商)提供    <namespace>www.jbaptech.com.cn/CreditCard</namespace>        为name元素绑定的class类 如果是接口就要下一元素指定接口的实现类    <serviceClass>y2javaee.sg.ch09.IProcessCredit</serviceClass>        接口的实现类    <implementationClass>y2javaee.sg.ch09.ProcessCredit</implementationClass>  </service> -->   </beans>

    -----------------------服务接口--------------------------

    package ch09.server;

    import java.util.Random;

    public class WeatherImpl implements Iweather {//实现随机数接口的类 返回1-15的随机数 public int getRandom() {  Random rand=new Random();  int radom=rand.nextInt(16);//产生1~16的整数nextInt(100)时返回0-99  return radom; }

    }

    ----------------------------客户端调用-------------------------

    package ch09.client;

    import org.codehaus.xfire.XFireFactory;import org.codehaus.xfire.client.XFireProxyFactory;import org.codehaus.xfire.service.Service;import org.codehaus.xfire.service.binding.ObjectServiceFactory;

    import ch09.server.Iweather;//天气的客户端类public class WeatherClient {public int ramGain() {   int result=-1;//代表运行本方法出错时的默认值     //创建天气预报web服务的元数据                      //实例化对象服务工厂并调用它的Crete方法传入参数(接口类) 获得服务的元数据   Service srvcModel=new ObjectServiceFactory().create(Iweather.class);//括号里面获得接口(类)    //创建天气预报web服务的代理                   //实例化XFire的代理工厂XFireProxyFactory(XFireFactory.newInstance()获得xFire库的实例,getXFire()通过内部方法获得库)   XFireProxyFactory  factory=new XFireProxyFactory(XFireFactory.newInstance().getXFire());     //创建天气预报web服务的地址(这里写的是本机)    /*(http://localhost:8080/restranthgk/services/weatherService?wsdl)利用此地址测试是否成功    * services/*为web.xml配置文件拦截下来访问的路径  weatherService为 services.xml里面的名    * 路径对应正确才能成功  每次更改配置文件时都要重启tomcat 否则还是运行原来的代码    */   String helloWorldURL="http://localhost:8080/restranthgk/services/weatherService";      try {  //生成天气预报web服务调用对象                 //(强制把object类型转换为接口类型)通过代理工厂factory.create(元数据,服务URL)    Iweather iClient=(Iweather)factory.create(srvcModel,helloWorldURL);  //获得0-15的随机整数    result=iClient.getRandom();//通过接口调用方法获得随机数 } catch (Exception e) {  }  return result;//返回}//显示天气最终的状况public  String showWeather() {  int weatherNum=this.ramGain();//调用上面的方法  String todayWeather="";  switch (weatherNum) {  case 0:   todayWeather="晴";   break;  case 1:   todayWeather="晴到多云";   break;  case 2:   todayWeather="多云";   break;  case 3:   todayWeather="阵雨";   break;  case 4:   todayWeather="中雨";   break;  case 5:   todayWeather="大雨";   break;  case 6:   todayWeather="大到暴雨";   break;  case 7:   todayWeather="雷阵雨";   break;  case 8:   todayWeather="雨夹雪";   break;  case 9:   todayWeather="小雪";   break;  case 10:   todayWeather="中雪";   break;  case 11:   todayWeather="大到暴雪";   break;  case 12:   todayWeather="冰雹";   break;  case 13:   todayWeather="霜冻";   break;  case 14:   todayWeather="雾";   break;  case 15:   todayWeather="阴";   break;  default:   todayWeather="预测不到,可能因为调用天气的API出错了";   break;  }  return todayWeather;    }}----------------------------------------------------


    最新回复(0)