Joffice2中WebService的使用(CXF)

1.    WebService基本概念

WSDL:
        http://www.w3cschool.cn/index-20.html
    SOAP:
        http://www.w3school.com.cn/soap/index.asp

2.    引入CXF依赖库

   下载:http://cxf.apache.org/,解压至目录
   加上依赖的jar库,如:

Java代码   Joffice2中WebService的使用(CXF)
  1. commons-logging-1.1.jar  
  2. geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar)  
  3. geronimo-annotation_1.0_spec-1.1.jar (JSR 250)  
  4. geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar)  
  5. geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar)  
  6. geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181)  
  7. jaxb-api-2.0.jar  
  8. jaxb-impl-2.0.5.jar  
  9. jaxws-api-2.0.jar  
  10. neethi-2.0.jar  
  11. saaj-api-1.3.jar  
  12. saaj-impl-1.3.jar  
  13. stax-api-1.0.1.jar  
  14. wsdl4j-1.6.1.jar  
  15. wstx-asl-3.2.1.jar  
  16. XmlSchema-1.2.jar  
  17. xml-resolver-1.2.jar  
 


参考:http://cxf.apache.org/docs/writing-a-service-with-spring.html

 

  3.    发布J.Office的Service类为Service

 

  编写RegService接口及实现类,如下所示:(注意@WebService)

  3.1        RegService接口

 

Java代码   Joffice2中WebService的使用(CXF)
  1. package com.htsoft.oa.service.cxf;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.jws.WebService;  
  6.   
  7. import com.htsoft.oa.model.admin.Regulation;  
  8.   
  9. @WebService (targetNamespace="http://www.jee-soft.cn")  
  10. public interface RegService {  
  11.     public List<Regulation> getAll();  
  12. }  

 3. 2 RegServiceImpl

 

 

Java代码   Joffice2中WebService的使用(CXF)
  1. package com.htsoft.oa.service.cxf.impl;  
  2.   
  3. import java.util.List;  
  4.   
  5. import javax.annotation.Resource;  
  6. import javax.jws.WebService;  
  7.   
  8. import com.htsoft.oa.model.admin.Regulation;  
  9. import com.htsoft.oa.service.admin.RegulationService;  
  10. import com.htsoft.oa.service.cxf.RegService;  
  11.   
  12. @WebService   
  13. public class RegServiceImpl implements RegService{  
  14.     @Resource  
  15.     RegulationService regulationService;  
  16.       
  17.     @Override  
  18.     public  List<Regulation> getAll() {  
  19.         return regulationService.getAll();  
  20.     }  
  21. }  

 4.    在J.Office中加上对外的发布Service

在resources/conf下加一app-cxf.xml,内容如下:

Java代码   Joffice2中WebService的使用(CXF)
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:jaxws="http://cxf.apache.org/jaxws"  
  5.     xsi:schemaLocation="  
  6.     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">  
  8.   
  9.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  10.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />   
  11.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  12.       
  13.     <bean id="regService" class="com.htsoft.oa.service.cxf.impl.RegServiceImpl"/>  
  14.       
  15.     <jaxws:endpoint id="regServiceWs" implementor="#regService"   
  16.         address="/regService" />  
  17.       
  18. </beans>  

 并且在app-context.xml中引入以上文件

<import resource="app-cxf.xml"/>

在web.xml中加入

 

 

Java代码   Joffice2中WebService的使用(CXF)
  1. <!-- cxf 服务器-->  
  2.     <servlet>  
  3.         <servlet-name>CXFServlet</servlet-name>  
  4.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
  5.         <load-on-startup>1</load-on-startup>  
  6.     </servlet>  
  7.   
  8.     <servlet-mapping>  
  9.         <servlet-name>CXFServlet</servlet-name>  
  10.         <url-pattern>/service/*</url-pattern>  
  11.     </servlet-mapping>  

 

启动后,则可以访问路径:http://localhost:8080/joffice21/ws/regService?wsdl
效果如下:

 Joffice2中WebService的使用(CXF)

执行ant任务,把joffice中的类打包成ht_cxf_client.jar放置客户端环境:

 

Java代码   Joffice2中WebService的使用(CXF)
  1. <target name="jarservice-model">  
  2.         <jar destfile="build/ht_cxf_client.jar">  
  3.             <fileset dir="web/WEB-INF/classes">  
  4.                 <include name="com/htsoft/core/**"/>  
  5.                 <include name="com/htsoft/oa/service/**"/>  
  6.                 <include name="com/htsoft/oa/model/**"/>  
  7.                 <exclude name="com/htsoft/oa/model/**/*.hbm.xml"/>  
  8.             </fileset>  
  9.         </jar>  
  10.     </target>  

 5.    编写测试客户端

 

Java代码   Joffice2中WebService的使用(CXF)
  1. package com.cxf;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  6.   
  7. import com.htsoft.oa.model.admin.Regulation;  
  8. import com.htsoft.oa.service.cxf.RegService;  
  9.   
  10. public class ClientMain {  
  11.   
  12.     /** 
  13.      * @param args 
  14.      */  
  15.     public static void main(String[] args) {  
  16.         // TODO Auto-generated method stub  
  17.         JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean();  
  18.         factoryBean.setAddress("http://localhost:8080/joffice21/ws/regService");  
  19.         factoryBean.setServiceClass(RegService.class);  
  20.           
  21.         RegService regService=(RegService)factoryBean.create();  
  22.           
  23.         List<Regulation> list=regService.getAll();  
  24.           
  25.         if(list!=null){  
  26.             for(Regulation reg:list){  
  27.                 System.out.println(" reg:" + reg.getSubject());  
  28.             }  
  29.         }  
  30.     }  
  31.   

转载于:https://my.oschina.net/u/587349/blog/71060