很多时候,我们需要创建WebService供外部系统调用。这个时候一种好的WebService解决方案非常重要。下面介绍Spring结合CXF创建WebService的步骤以及如何创建客户端测试。
第一步,需要引用相关包,这里已经将所有需要引用的包结构打包了,如下:
所需导入的包下载:http://www.javaxxz.com/file.php?id=15524516
第二步,在web.xml中配置Spring和CXFServlet如下:
//从WEB-INF路径下加载Spring配置文件。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/springcfg/applicationContext.xml</param-value>
</context-param>
//负责启动Spring容器的监听器,它将引用上方的contextConfigLocation参数获得Spring配置文件地址。
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
//配置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>/*</url-pattern>
</servlet-mapping>
第三步:创建dao,service,action三层架构中类,并配置依赖关系
比如:CustomerDao,CustomerService,CustomerAction以及它们的实现类。
其中CustomerAction接口类用于发布,所以在类前声明:@WebService。CXF便知道这个类为Service服务类。
在Spring配置文件中,配置三层架构的依赖关系。
第四步:在Spring配置文件中配置CXF WebService服务。
配置文件spring-webservice.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:context=”http://www.springframework.org/schema/context”
xmlns:aop=”http://www.springframework.org/schema/aop”
xmlns:tx=”http://www.springframework.org/schema/tx”
xmlns:jaxws=”http://cxf.apache.org/jaxws”
xmlns:cxf=”http://cxf.apache.org/core”
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://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core
http://cxf.apache.org/schemas/core.xsd”>
//引入CXF默认配置
<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” />
//IP限制访问拦截器
<bean id=”accessInterceptor” class=”****.AccessInterceptor”/>
//IP限制访问拦截器配置
<cxf:bus>
<cxf:inInterceptors>
<ref bean=”accessInterceptor” />
</cxf:inInterceptors>
</cxf:bus>
//配置Customer Service类
<bean id=”customerAction” class=”****.CustomerActionImpl” scope=”prototype”>
<property name=”customerService” ref=”customerService”/>
</bean>
//发布Customer服务配置
<jaxws:endpoint id=”customer” implementor=”#customerAction” address=”/customer” publish=”true” />
</beans>
至此解决方案完成!直接启动tomcat便可访问WebService接口。
更多:http://www.javaxxz.com/thread-67142-1-1.html
浙公网安备 33010602011771号