溫暖潔森

          勇敢做自己

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

          最近一直使用XFire做WebServices方式使用,總結(jié)一些相關(guān)經(jīng)驗(yàn),本文以具體例子進(jìn)行XFire開發(fā)過程分析

          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、實(shí)現(xiàn)上述接口,實(shí)現(xiàn)業(yè)務(wù)邏輯

           

           

           

          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 創(chuàng)建時間: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集合,需要對集合中的元素進(jìn)行定義,在這里是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結(jié)合一起使用的,所以用到了spring相關(guān)內(nèi)容。
          注意:位置一定要在WEB-INF目錄下

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

          <?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 閱讀(4372) 評論(0)  編輯  收藏 所屬分類: J2EE
          主站蜘蛛池模板: 法库县| 凤台县| 福贡县| 萝北县| 巴东县| 永平县| 四会市| 永和县| 贡觉县| 磴口县| 偏关县| 芜湖市| 南通市| 冷水江市| 新龙县| 剑川县| 西畴县| 隆子县| 凤冈县| 湛江市| 监利县| 兰考县| 二手房| 犍为县| 延津县| 建昌县| 古蔺县| 鄂伦春自治旗| 武功县| 浦城县| 平罗县| 岳西县| 同德县| 冀州市| 柘荣县| 邓州市| 吉首市| 廉江市| 衡山县| 全椒县| 平乐县|