溫暖潔森

          勇敢做自己

            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 閱讀(4372) 評論(0)  編輯  收藏 所屬分類: J2EE
          主站蜘蛛池模板: 石屏县| 巢湖市| 上饶市| 武功县| 禹州市| 伊宁县| 巴林左旗| 葵青区| 方城县| 洞头县| 民和| 保康县| 娱乐| 开平市| 中阳县| 潮州市| 永平县| 古交市| 南江县| 博乐市| 柳河县| 宝山区| 云安县| 池州市| 右玉县| 烟台市| 黄浦区| 宝山区| 镇安县| 永寿县| 湛江市| 卓资县| 顺义区| 察隅县| 中江县| 包头市| 綦江县| 太湖县| 阿克陶县| 丹寨县| 桐庐县|