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库,如:
- commons-logging-1.1.jar ??
- geronimo-activation_1.1_spec-1.0-M1.jar?(or?Sun's?Activation?jar) ??
- geronimo-annotation_1.0_spec-1.1.jar?(JSR?250) ??
- geronimo-javamail_1.4_spec-1.0-M1.jar?(or?Sun's?JavaMail?jar) ??
- geronimo-servlet_2.5_spec-1.1-M1.jar?(or?Sun's?Servlet?jar) ??
- geronimo-ws-metadata_2.0_spec-1.1.1.jar?(JSR?181) ??
- jaxb-api-2.0.jar ??
- jaxb-impl-2.0.5.jar ??
- jaxws-api-2.0.jar ??
- neethi-2.0.jar ??
- saaj-api-1.3.jar ??
- saaj-impl-1.3.jar ??
- stax-api-1.0.1.jar ??
- wsdl4j-1.6.1.jar ??
- wstx-asl-3.2.1.jar ??
- XmlSchema-1.2.jar ??
- xml-resolver-1.2.jar??
commons-logging-1.1.jar geronimo-activation_1.1_spec-1.0-M1.jar (or Sun's Activation jar) geronimo-annotation_1.0_spec-1.1.jar (JSR 250) geronimo-javamail_1.4_spec-1.0-M1.jar (or Sun's JavaMail jar) geronimo-servlet_2.5_spec-1.1-M1.jar (or Sun's Servlet jar) geronimo-ws-metadata_2.0_spec-1.1.1.jar (JSR 181) jaxb-api-2.0.jar jaxb-impl-2.0.5.jar jaxws-api-2.0.jar neethi-2.0.jar saaj-api-1.3.jar saaj-impl-1.3.jar stax-api-1.0.1.jar wsdl4j-1.6.1.jar wstx-asl-3.2.1.jar XmlSchema-1.2.jar 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接口
?
- package?com.htsoft.oa.service.cxf; ??
- ??
- import?java.util.List; ??
- ??
- import?javax.jws.WebService; ??
- ??
- import?com.htsoft.oa.model.admin.Regulation; ??
- ??
- @WebService(targetNamespace="http://www.jee-soft.cn") ??
- public?interface?RegService?{ ??
- ????public?List<Regulation>?getAll(); ??
- }??
package com.htsoft.oa.service.cxf; import java.util.List; import javax.jws.WebService; import com.htsoft.oa.model.admin.Regulation; @WebService(targetNamespace="http://www.jee-soft.cn") public interface RegService { public List<Regulation> getAll(); }
?3. 2 RegServiceImpl
?
?
- package?com.htsoft.oa.service.cxf.impl; ??
- ??
- import?java.util.List; ??
- ??
- import?javax.annotation.Resource; ??
- import?javax.jws.WebService; ??
- ??
- import?com.htsoft.oa.model.admin.Regulation; ??
- import?com.htsoft.oa.service.admin.RegulationService; ??
- import?com.htsoft.oa.service.cxf.RegService; ??
- ??
- @WebService??
- public?class?RegServiceImpl?implements?RegService{ ??
- ????@Resource??
- ????RegulationService?regulationService; ??
- ???? ??
- ????@Override??
- ????public??List<Regulation>?getAll()?{ ??
- ????????return?regulationService.getAll(); ??
- ????} ??
- }??
package com.htsoft.oa.service.cxf.impl; import java.util.List; import javax.annotation.Resource; import javax.jws.WebService; import com.htsoft.oa.model.admin.Regulation; import com.htsoft.oa.service.admin.RegulationService; import com.htsoft.oa.service.cxf.RegService; @WebService public class RegServiceImpl implements RegService{ @Resource RegulationService regulationService; @Override public List<Regulation> getAll() { return regulationService.getAll(); } }
?4.?? ?在J.Office中加上对外的发布Service
在resources/conf下加一app-cxf.xml,内容如下:
- <?xml?version="1.0"?encoding="UTF-8"?> ??
- <beans?xmlns="http://www.springframework.org/schema/beans"??
- ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
- ????xmlns:jaxws="http://cxf.apache.org/jaxws"??
- ????xsi:schemaLocation=" ??
- ????http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans.xsd ??
- ????http://cxf.apache.org/jaxws?http://cxf.apache.org/schemas/jaxws.xsd"> ??
- ??
- ????<import?resource="classpath:META-INF/cxf/cxf.xml"?/> ??
- ????<import?resource="classpath:META-INF/cxf/cxf-extension-soap.xml"?/>? ??
- ????<import?resource="classpath:META-INF/cxf/cxf-servlet.xml"?/> ??
- ???? ??
- ????<bean?id="regService"?class="com.htsoft.oa.service.cxf.impl.RegServiceImpl"/> ??
- ???? ??
- ????<jaxws:endpoint?id="regServiceWs"?implementor="#regService"? ??
- ????????address="/regService"?/> ??
- ???? ??
- </beans>??
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <bean id="regService" class="com.htsoft.oa.service.cxf.impl.RegServiceImpl"/> <jaxws:endpoint id="regServiceWs" implementor="#regService" address="/regService" /> </beans>
?并且在app-context.xml中引入以上文件
<import resource="app-cxf.xml"/>
在web.xml中加入
?
?
- <!--?cxf?服务器--> ??
- ????<servlet> ??
- ????????<servlet-name>CXFServlet</servlet-name> ??
- ????????<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> ??
- ????????<load-on-startup>1</load-on-startup> ??
- ????</servlet> ??
- ??
- ????<servlet-mapping> ??
- ????????<servlet-name>CXFServlet</servlet-name> ??
- ????????<url-pattern>/service/*</url-pattern> ??
- ????</servlet-mapping>??
<!-- cxf 服务器--> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping>
?
启动后,则可以访问路径:http://localhost:8080/joffice21/ws/regService?wsdl
效果如下:
?
执行ant任务,把joffice中的类打包成ht_cxf_client.jar放置客户端环境:
?
- <target?name="jarservice-model"> ??
- ????????<jar?destfile="build/ht_cxf_client.jar"> ??
- ????????????<fileset?dir="web/WEB-INF/classes"> ??
- ????????????????<include?name="com/htsoft/core/**"/> ??
- ????????????????<include?name="com/htsoft/oa/service/**"/> ??
- ????????????????<include?name="com/htsoft/oa/model/**"/> ??
- ????????????????<exclude?name="com/htsoft/oa/model/**/*.hbm.xml"/> ??
- ????????????</fileset> ??
- ????????</jar> ??
- ????</target>??
<target name="jarservice-model"> <jar destfile="build/ht_cxf_client.jar"> <fileset dir="web/WEB-INF/classes"> <include name="com/htsoft/core/**"/> <include name="com/htsoft/oa/service/**"/> <include name="com/htsoft/oa/model/**"/> <exclude name="com/htsoft/oa/model/**/*.hbm.xml"/> </fileset> </jar> </target>
?5.?? ?编写测试客户端
?
- package?com.cxf; ??
- ??
- import?java.util.List; ??
- ??
- import?org.apache.cxf.jaxws.JaxWsProxyFactoryBean; ??
- ??
- import?com.htsoft.oa.model.admin.Regulation; ??
- import?com.htsoft.oa.service.cxf.RegService; ??
- ??
- public?class?ClientMain?{ ??
- ??
- ????/** ?
- ?????*?@param?args ?
- ?????*/??
- ????public?static?void?main(String[]?args)?{ ??
- ????????//?TODO?Auto-generated?method?stub ??
- ????????JaxWsProxyFactoryBean?factoryBean=new?JaxWsProxyFactoryBean(); ??
- ????????factoryBean.setAddress("http://localhost:8080/joffice21/ws/regService"); ??
- ????????factoryBean.setServiceClass(RegService.class); ??
- ???????? ??
- ????????RegService?regService=(RegService)factoryBean.create(); ??
- ???????? ??
- ????????List<Regulation>?list=regService.getAll(); ??
- ???????? ??
- ????????if(list!=null){ ??
- ????????????for(Regulation?reg:list){ ??
- ????????????????System.out.println("?reg:"?+?reg.getSubject()); ??
- ????????????} ??
- ????????} ??
- ????} ??
- ??
- }??
package com.cxf; import java.util.List; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.htsoft.oa.model.admin.Regulation; import com.htsoft.oa.service.cxf.RegService; public class ClientMain { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JaxWsProxyFactoryBean factoryBean=new JaxWsProxyFactoryBean(); factoryBean.setAddress("http://localhost:8080/joffice21/ws/regService"); factoryBean.setServiceClass(RegService.class); RegService regService=(RegService)factoryBean.create(); List<Regulation> list=regService.getAll(); if(list!=null){ for(Regulation reg:list){ System.out.println(" reg:" + reg.getSubject()); } } } }?
?