当前位置: 代码迷 >> Web前端 >> JOffice2中WebService的应用(CXF)
  详细解决方案

JOffice2中WebService的应用(CXF)

热度:415   发布时间:2012-07-15 20:11:40.0
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代码 复制代码?收藏代码
  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??
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接口

?

Java代码 复制代码?收藏代码
  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. }??
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

?

?

Java代码 复制代码?收藏代码
  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. }??
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,内容如下:

Java代码 复制代码?收藏代码
  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>??
<?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中加入

?

?

Java代码 复制代码?收藏代码
  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>??
<!-- 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放置客户端环境:

?

Java代码 复制代码?收藏代码
  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>??
<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.?? ?编写测试客户端

?

Java代码 复制代码?收藏代码
  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. ??
  32. }??
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());
			}
		}
	}

}
?

?

  相关解决方案