<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 閱讀(4364) 評論(0)  編輯  收藏 所屬分類: J2EE
    主站蜘蛛池模板: 亚洲精品免费在线| 在线A亚洲老鸭窝天堂| 亚洲国产精品久久丫| 久久青草精品38国产免费| 亚洲国产美女精品久久久久∴| jizz在线免费观看| 亚洲午夜福利精品无码| 免费无码AV一区二区| 亚洲成人国产精品| 成年免费a级毛片| 国产亚洲精品拍拍拍拍拍| a毛片免费全部播放完整成| 久久九九亚洲精品| 免费看又黄又无码的网站| 亚洲视频一区网站| 免费av欧美国产在钱| 亚洲AV永久无码精品一福利 | 青青草a免费线观a| 亚洲精品国产国语| 国产色爽免费视频| 一区二区三区在线免费| 久久精品亚洲综合| 免费阿v网站在线观看g| 亚洲成a∨人片在无码2023| 亚洲国产一区二区三区| 在线观看肉片AV网站免费| 亚洲白色白色在线播放| 国产美女精品视频免费观看| 高清永久免费观看| 亚洲性色成人av天堂| 国产无遮挡吃胸膜奶免费看| sss日本免费完整版在线观看| 久久久亚洲欧洲日产国码是AV| 天天看免费高清影视| 最近免费中文字幕中文高清| 亚洲av无码片在线观看| 亚洲国产精品第一区二区三区| 国产白丝无码免费视频| 亚洲欧洲专线一区| 亚洲av无码成人黄网站在线观看| 成年美女黄网站18禁免费|