xfire+webservice(服务器配置篇)

来源:互联网 发布:淘宝基础版升级专业版 编辑:程序博客网 时间:2024/06/05 02:31

今天终于把webservice搞完了,写个总结,我是在原来的项目中,进行webservice扩展的。

    首先在已完成的项目中加入webservice支持,如图

    xfire+webservice(服务器配置篇)  - 气体分子 - 气体分子

 

下一步

    xfire+webservice(服务器配置篇)  - 气体分子 - 气体分子

    接下来选择xfire包

    xfire+webservice(服务器配置篇)  - 气体分子 - 气体分子

    finish 完成。

    这样在你的项目中会产生xfire的工具包,这里完全可以自己将所需要的包放入lib下。

    由于我的原始项目是ssh的,这里就不再说了,接下来进行xfire配置。

    首先web.xml

Xml代码
  1. <!-- begin XFire 配置 -->  
  2.   <servlet>      
  3.      <servlet-name>xfire</servlet-name>      
  4.      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
  5.   </servlet>      
  6.   <servlet-mapping>    
  7.      <servlet-name>xfire</servlet-name>  
  8.      <url-pattern>*.ws</url-pattern>  
  9.   </servlet-mapping>  
  10.      
  11. lt;!-- 配合Spring容器中XFire一起工作的Servlet-->     
  12.  <servlet>  
  13.   <servlet-name>XFireServlet</servlet-name>  
  14.   <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>  
  15. </servlet>  
  16. <servlet-mapping>  
  17.   <servlet-name>XFireServlet</servlet-name>  
  18.   <url-pattern>/servlet/XFireServlet/*</url-pattern>  
  19. </servlet-mapping>  
  20. <servlet-mapping>  
  21.   <servlet-name>XFireServlet</servlet-name>  
  22.   <url-pattern>/services/*</url-pattern>  
  23. </servlet-mapping>  
  <!-- begin XFire 配置 -->    <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>*.ws</url-pattern>    </servlet-mapping>     <!-- 配合Spring容器中XFire一起工作的Servlet-->     <servlet>    <servlet-name>XFireServlet</servlet-name>    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>  </servlet>  <servlet-mapping>    <servlet-name>XFireServlet</servlet-name>    <url-pattern>/servlet/XFireServlet/*</url-pattern>  </servlet-mapping>  <servlet-mapping>    <servlet-name>XFireServlet</servlet-name>    <url-pattern>/services/*</url-pattern>  </servlet-mapping>

 接下来在src下面建立xfire-servlet.xml

Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"   
  3.     "http://www.springframework.org/dtd/spring-beans.dtd">  
  4. <beans>  
  5.     <!-- 引入XFire预配置信息 -->  
  6.     <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />  
  7.     <!-- 定义访问的url -->  
  8.     <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">  
  9.        <property name="urlMap">                
  10.            <map>                    
  11.               <entry key="/webservices.ws">                     
  12.                   <ref bean="webservices" />                    
  13.               </entry>                
  14.            </map>            
  15.        </property>        
  16.     </bean>        
  17.   
  18.     <!-- 使用XFire导出器 -->  
  19.     <bean id="baseWebService" class="org.codehaus.xfire.spring.remoting.XFireExporter" lazy-init="false" abstract="true">  
  20.        <!-- 引用xfire.xml中定义的工厂 -->  
  21.        <property name="serviceFactory" ref="xfire.serviceFactory" />  
  22.        <!-- 引用xfire.xml中的xfire实例 -->  
  23.        <property name="xfire" ref="xfire" />  
  24.     </bean>  
  25.        
  26.     <bean id="webservices" parent="baseWebService">  
  27.        <!-- 业务服务bean -->  
  28.        <property name="serviceBean" ref="webservicesService" />  
  29.        <!-- 业务服务bean的窄接口类 -->  
  30.        <property name="serviceClass" value="com.seavision.huayi2.service.IWebservice" />  
  31.     </bean>  
  32. </beans>  
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"    "http://www.springframework.org/dtd/spring-beans.dtd"><beans>    <!-- 引入XFire预配置信息 -->    <import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />    <!-- 定义访问的url -->    <bean >       <property >                        <map>                               <entry key="/webservices.ws">                                    <ref bean="webservices" />                               </entry>                        </map>                </property>         </bean>         <!-- 使用XFire导出器 -->    <bean   lazy-init="false" abstract="true">       <!-- 引用xfire.xml中定义的工厂 -->       <property  ref="xfire.serviceFactory" />       <!-- 引用xfire.xml中的xfire实例 -->       <property  ref="xfire" />    </bean>        <bean  parent="baseWebService">       <!-- 业务服务bean -->       <property  ref="webservicesService" />       <!-- 业务服务bean的窄接口类 -->       <property  value="com.seavision.huayi2.service.IWebservice" />    </bean></beans>

      这里说说为什么定义窄接口,原因xfire会导出spring整个接口,不能控制那些暴露给用户,这样做就不会将所有接口暴露给用户。

    下来说说,在项目下生成的services.xml,这个里面是按照spring2.0的命名空间配置的,所以改写这个xml

    

Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans>  
  3. </beans>  
<?xml version="1.0" encoding="UTF-8"?><beans></beans>

 这样就不会出错了。

 这样启动Tomcat,如果启动时抛以下异常,则删掉发布后lib中的spring1.2版本,原因与spring2冲突

 org.xml.sax.SAXParseException: Document root element "beans", must match DOCTYPE root "null"还有一些就不多说了。

 服务器启动后,在项目名后面键入/services 则出现wsdl,另存为即可。

 

 看看我的spring配置文件 ,和上面的xfire-servlet.xml对应起来。

Xml代码
  1. <!-- webservice -->  
  2.     <bean id="iwebserviceTarget" class="com.seavision.huayi2.service.impl.WebserviceImpl">  
  3.          <property name="businsesslogDAO">  
  4.             <ref local="TBusinsessLogDAO"/>  
  5.         </property>  
  6.         <property name="sationmonthreportDAO">  
  7.             <ref local="TStationMonthReportDAO"/>  
  8.         </property>          
  9.     </bean>  
  10.      <bean id="webservicesService" parent="txProxyTemplate">  
  11.         <property name="target" ref="iwebserviceTarget">  
  12.         </property>  
  13.     </bean>  
<!-- webservice --> <bean  >         <property >   <ref local="TBusinsessLogDAO"/>  </property>  <property >   <ref local="TStationMonthReportDAO"/>  </property>   </bean>     <bean  parent="txProxyTemplate">  <property  ref="iwebserviceTarget">  </property> </bean>

 

    service即实现类如下,此代码仅为本人项目代码,仅供参考,切勿抄送

   

Java代码
  1. public interface IWebservice {   
  2.     /***************  
  3.      * 取得基础数据表中所有数据,以基础数据建立日期的倒叙排序(HY0703)  
  4.      * @throws DAOException   
  5.      * ******************/  
  6.     public List<TBusinsessLog> getYeWuList();   
  7.     /*******************************  
  8.     *增加工作站月工作计划信息  
  9.     ********************************/  
  10.     public boolean insertYueJiHua(TStationMonthReport tStationMonthReport);   
  11. }  
public interface IWebservice { /***************  * 取得基础数据表中所有数据,以基础数据建立日期的倒叙排序(HY0703)  * @throws DAOException   * ******************/ public List<TBusinsessLog> getYeWuList(); /******************************* *增加工作站月工作计划信息 ********************************/ public boolean insertYueJiHua(TStationMonthReport tStationMonthReport);}

 实现类

Java代码
  1. public class WebserviceImpl implements IWebservice{   
  2.     TBusinsessLogDAO  businsesslogDAO;   
  3.     TStationMonthReportDAO    sationmonthreportDAO;   
  4.   
  5.        
  6.     public TStationMonthReportDAO getSationmonthreportDAO() {   
  7.         return sationmonthreportDAO;   
  8.     }   
  9.   
  10.   
  11.     public void setSationmonthreportDAO(TStationMonthReportDAO sationmonthreportDAO) {   
  12.         this.sationmonthreportDAO = sationmonthreportDAO;   
  13.     }   
  14.   
  15.   
  16.     public TBusinsessLogDAO getBusinsesslogDAO() {   
  17.         return businsesslogDAO;   
  18.     }   
  19.   
  20.   
  21.     public void setBusinsesslogDAO(TBusinsessLogDAO businsesslogDAO) {   
  22.         this.businsesslogDAO = businsesslogDAO;   
  23.     }   
  24.   
  25.   
  26.     public List<TBusinsessLog> getYeWuList(){   
  27.         System.out.println("调用ok");   
  28.         String hql="from TBusinsessLog as t order by t.cretateDate desc";   
  29.         List<TBusinsessLog> list= new ArrayList<TBusinsessLog>();   
  30.         try {   
  31.             List blist=businsesslogDAO.find(hql);   
  32.             for(int i=0;i<blist.size();i++){   
  33.                 list.add((TBusinsessLog) blist.get(i));   
  34.             }   
  35.         } catch (DAOException e) {   
  36.             // TODO Auto-generated catch block   
  37.             e.printStackTrace();   
  38.         }   
  39.         return list;   
  40.     }   
  41.        
  42.     /*******************************  
  43.     *增加工作站月工作计划信息  
  44.     ********************************/  
  45.     public boolean insertYueJiHua(TStationMonthReport tStationMonthReport) {       
  46.         System.out.println("调用ok");   
  47.         try {   
  48.             System.out.println("++++++++++"+tStationMonthReport.getStationMonthReportId());   
  49.             sationmonthreportDAO.save(tStationMonthReport);   
  50.             return true;   
  51.         } catch (DAOException e) {   
  52.                
  53.             e.printStackTrace();   
  54.             return false;   
  55.         }   
  56.   
  57.     }  
public class WebserviceImpl implements IWebservice{ TBusinsessLogDAO  businsesslogDAO; TStationMonthReportDAO    sationmonthreportDAO;  public TStationMonthReportDAO getSationmonthreportDAO() {  return sationmonthreportDAO; } public void setSationmonthreportDAO(TStationMonthReportDAO sationmonthreportDAO) {  this.sationmonthreportDAO = sationmonthreportDAO; } public TBusinsessLogDAO getBusinsesslogDAO() {  return businsesslogDAO; } public void setBusinsesslogDAO(TBusinsessLogDAO businsesslogDAO) {  this.businsesslogDAO = businsesslogDAO; } public List<TBusinsessLog> getYeWuList(){  System.out.println("调用ok");  String hql="from TBusinsessLog as t order by t.cretateDate desc";  List<TBusinsessLog> list= new ArrayList<TBusinsessLog>();  try {   List blist=businsesslogDAO.find(hql);   for(int i=0;i<blist.size();i++){    list.add((TBusinsessLog) blist.get(i));   }  } catch (DAOException e) {   // TODO Auto-generated catch block   e.printStackTrace();  }  return list; }  /******************************* *增加工作站月工作计划信息 ********************************/ public boolean insertYueJiHua(TStationMonthReport tStationMonthReport) {   System.out.println("调用ok");  try {   System.out.println("++++++++++"+tStationMonthReport.getStationMonthReportId());   sationmonthreportDAO.save(tStationMonthReport);   return true;  } catch (DAOException e) {      e.printStackTrace();   return false;  } }

 dao层就不说了,到此完毕

原创粉丝点击