记录用CXF开发WebService的过程经历和总结
问题一:如何选择CXF版本?
CXF自带Spring的相关JAR包,若是新项目,可直接从CXF中引用;若是与现有项目(含Spring框架)的进行整合,需要认真选择版本,以CXF中的Spring版本一致,或略高一些,一般是可以使用的。
问题二:CXF服务端的加载问题?
按官方文档的配置,需要在web中增加servlet,如下:
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
spring的前端控制器由DispatcherServlet管理,CXF的中若使用spring依赖注入的对象,最好是将cxf单独配置一个文件,例如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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
default-lazy-init="true">
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<bean id="newsTransferServiceImpl" class="com.gcms.cms.webservice.impl.NewsTransferServiceImpl"></bean>
<jaxws:endpoint id="newsTransferService" implementor="#newsTransferServiceImpl" address="/newsWS" publish="true">
<jaxws:features>
<bean class="org.apache.cxf.feature.LoggingFeature"/>
</jaxws:features>
</jaxws:endpoint>
</beans>
处理webservice请求的class一定要单独配置,并且通过implementor引入”#bean对应的ID“,这样才能正确找到wsdl。
问题三:框架中有Hibernate,多表关联时处理的注意事项。
所有的逻辑处理,都要在service层中进行,不能直接在webservice的类中调用数据库,因为存在fetch策略和lazy加载的异常。