<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    溫暖潔森

    勇敢做自己

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      13 隨筆 :: 1 文章 :: 70 評論 :: 0 Trackbacks

    最近一直使用XFire做WebServices方式使用,總結一些相關經驗,本文以具體例子進行XFire開發過程分析

    1、首先定義一個Interface,具體包含簡單類型傳值、返回自定義對象、返回集合三個接口方法

    import java.util.List;

    import com.gresoft.commons.core.exception.BusinessException;
    import com.gresoft.sanitation.model.dictionary.Hospital;

    public interface WebServicesInterface {
     public String savePutXml(int i,String typecode) throws BusinessException;
     public Hospital getList(int p);
     public List getAll();
    }


    2、實現上述接口,實現業務邏輯

     

     

     

    public class HospitalService extends HibernateEntityDao<Hospital> implements
      WebServicesInterface {

     private HospitalManager hospitalManager;

     public void setHospitalManager(HospitalManager hospitalManager) {
      this.hospitalManager = hospitalManager;
     }

     public String savePutXml(int o, String typecode) throws BusinessException {
      // TODO Auto-generated method stub
      Hospital h = new Hospital(o, typecode);
      super.save(h);
      System.out.println("ccccc");
      return "gggg";
     }

     public Hospital getList(int p) {
      // TODO Auto-generated method stub

      Hospital h = hospitalManager.get(p);
      System.out.println("name:" + h.getHospitalName());
      return hospitalManager.get(p);

     }

     @SuppressWarnings("unchecked")
     public List getAll() {
      List list = hospitalManager.getAll();
      System.out.println("size:" + list.size());
      return hospitalManager.getAll();
     }

    }

    定義自定義對象Hospital

     

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.Id;
    import javax.persistence.Table;

    import org.hibernate.annotations.Cache;
    import org.hibernate.annotations.CacheConcurrencyStrategy;

    /**
     * @author wuxj:
     * @version 創建時間:2007-10-15 上午11:31:39
     */
    @Entity
    @Table(name = "HOSPITAL", schema = "DB2ADMIN")
    @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
    public class Hospital implements java.io.Serializable {
     private int hospitalId;

     private String hospitalName;

     public Hospital() {

     }

     public Hospital(int hospitalId) {
      this.hospitalId = hospitalId;
     }

     public Hospital(int hospitalId, String hospitalName) {
      this.hospitalId = hospitalId;
      this.hospitalName = hospitalName;
     }

     @Id
     @Column(name = "HOSPITAL_ID", unique = true, nullable = false)
     public int getHospitalId() {
      return hospitalId;
     }

     public void setHospitalId(int hospitalId) {
      this.hospitalId = hospitalId;
     }

     @Column(name = "HOSPITAL_NAME", nullable = false, length = 20)
     public String getHospitalName() {
      return hospitalName;
     }

     public void setHospitalName(String hospitalName) {
      this.hospitalName = hospitalName;
     }

    }


    3、編寫XFire的描述文件

    描述文件為WebServicesInterface.aegis.xml,一定要與Interface接口類在同一個目錄,否則會出錯誤
    只需要描述返回是集合的描述,getAll方法返回是List集合,需要對集合中的元素進行定義,在這里是Hospital對象

    <?xml version="1.0" encoding="UTF-8"?>
    <mappings>
     <mapping>

      <method name="getAll">
       <return-type
        componentType="com.gresoft.sanitation.model.dictionary.Hospital" />
      </method>
        <property name="hibernateGenericDao" ignore="true" />

      <!-- 在HospitalService中這個文件可以不用,主要是為了定義返回為對象或集合 -->
      <!--
      <method name="savePutXml">
       <parameter index="1" componentType="java.lang.String" />
      </method>
      -->
     </mapping>
    </mappings>


    4、編寫xfire-servlet.xml文件

    xfire是跟spring結合一起使用的,所以用到了spring相關內容。
    注意:位置一定要在WEB-INF目錄下

    key="/echo"中的echo為你在客戶端訪問的地址,具體看client內容

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>

     <!-- START SNIPPET: xfire -->
     <bean
      class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <property name="urlMap">
       <map>
        <entry key="/echo" value-ref="echo" />
        </map>
      </property>
     </bean>

     <!-- Declare a parent bean with all properties common to both services -->
     <bean id="echo"
      class="org.codehaus.xfire.spring.remoting.XFireExporter">
      <property name="serviceFactory" ref="xfire.serviceFactory" />
      <property name="xfire" ref="xfire" />
      <property name="serviceBean" ref="hospitalService" />
      <property name="serviceInterface"
       value="com.gresoft.sanitation.service.WebServicesInterface" />
       </bean>

     <!-- END SNIPPET: xfire -->
    </beans>


    5、編寫客戶端測試

     

    import java.net.MalformedURLException;

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

     

    public class HospitalServiceClient {
     public static void main(String args[]) {
      Service srvcModel = new ObjectServiceFactory()
        .create(WebServicesInterface.class);
      XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
        .newInstance().getXFire());
      // XFireProxyFactory factory = new XFireProxyFactory(getXFire());

      String helloWorldURL = "http://localhost:8080/sanitation/service/echo";
      // String helloWorldURL ="xfire.local://echo";
      try {
       WebServicesInterface srvc = (WebServicesInterface) factory.create(
         srvcModel, helloWorldURL);

       Hospital h = new Hospital();
       h.setHospitalId(11);
       h.setHospitalName("測試");
       // System.out.print(srvc.sayHello("wuxj")+"\n");
       // RegisterApply apply = new RegisterApply();
       // apply.setOperateDate("20071221");
       // apply.setRegisterId("1000");

       // srvc.savePutXml(apply);

       System.out.println(srvc.savePutXml(11, "測試"));
       srvc.getList(1);
       srvc.getAll();

       // System.out.print(((MsdpUser)srvc.getUser().get(0)).getUid());
      } catch (MalformedURLException e) {
       System.out.println("wrong");
       e.printStackTrace();
      }

     }

    }


    6、定義web.xml的xfire

    紅色字體一定要加上,而且是固定格式,這是xfire需要加載的文件
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
       xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath*:spring/*.xml,classpath:org/codehaus/xfire/spring/xfire.xml</param-value>
     </context-param>
    <!--Spring ApplicationContext 載入 -->
     <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
     </listener>

    <!-- XFire WebServices的入口 -->
     <servlet>
      <servlet-name>xfire</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
     </servlet>

     <servlet-mapping>
      <servlet-name>xfire</servlet-name>
      <url-pattern>/service/*</url-pattern>
     </servlet-mapping>

    </web-app>
    posted on 2008-01-04 10:54 harry520 閱讀(4373) 評論(0)  編輯  收藏 所屬分類: J2EE
    主站蜘蛛池模板: 亚洲爆乳精品无码一区二区三区 | 亚洲一本一道一区二区三区| 78成人精品电影在线播放日韩精品电影一区亚洲| 亚洲一区AV无码少妇电影☆| 亚洲永久永久永久永久永久精品| 亚洲精品国产免费| 色婷婷六月亚洲综合香蕉| 岛国精品一区免费视频在线观看| 日本亚洲欧洲免费天堂午夜看片女人员 | 欧洲亚洲综合一区二区三区| 拍拍拍无挡免费视频网站| 亚洲啪啪免费视频| 在线观着免费观看国产黄| 亚洲精品国产V片在线观看| 亚洲伦理一区二区| 成年大片免费高清在线看黄| 精品一区二区三区免费毛片爱| 在线jyzzjyzz免费视频| 亚洲乱码国产一区三区| 亚洲成aⅴ人片久青草影院按摩| 99精品免费视品| 亚洲国产精品VA在线看黑人| 免费一区二区无码东京热| 日韩免费毛片视频| 国产成人高清亚洲一区久久| 91视频免费网址| 中文字幕亚洲男人的天堂网络 | 国产亚洲av片在线观看播放 | 国产又黄又爽胸又大免费视频| 亚洲精品乱码久久久久久自慰| 精品国产麻豆免费人成网站| va亚洲va日韩不卡在线观看| 亚洲欧洲日韩国产综合在线二区| 2021精品国产品免费观看| 亚洲日韩欧洲无码av夜夜摸| 91精品视频在线免费观看| 亚洲欧美日韩综合俺去了| 日韩吃奶摸下AA片免费观看| 亚洲AV天天做在线观看| 久久av免费天堂小草播放| 日产乱码一卡二卡三免费|