【WebService框架-CXF】——CXF+Spring+自定义拦截器构建WebServ
步骤1.新建Java Web Project,引入CXFjar包(不包括Jetty和Servlet)
2.引入Springjar包(如果已经是SSH项目,此步可略过) 3.编写web.xml文件,配置监听器 <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/wsContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--在Web应用启动时加载Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
4.在applicationContext.xml中配置shema 需要找到CXF所在的“包”——targetNamespace http://cxf.apache.org/jaxws 以及CXF约束定义的物理文件 http://cxf.apache.org/schemas/jaxws.xsd=schemas/jaxws.xsd <beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
>
5.在applicationContext.xml中引入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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.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"/>
</beans>
6.在applicationContext.xml配置文件中配置bean和发布的终结点地址以及拦截器 <bean id="userService" class="com.tgb.service.impl.UserServiceImpl"/>
<bean id="HelloWorldWS" class="com.tgb.ws.impl.HelloWorldWS" >
<property name="userService" ref="userService"></property>
</bean>
<jaxws:endpoint implementor="#HelloWorldWS" address="/HelloWorldWS">
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
<bean class="com.tgb.ws.auth.AuthInterceptor"></bean>
</jaxws:inInterceptors>
</jaxws:endpoint>
这里注意implementor的名称可以为类的全称,也可以是配置到容器中的bean的id名称,前面加个“#”。 7.在web.xml中配置CXF的Servlet和访问路径 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!--在Web应用启动时加载Spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXF</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXF</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
8.在Tomcat中部署项目并启动,访问http://localhost:8080/CXF_Spring/webservice 我们可以访问http://localhost:8080/CXF_Spring/webservice/HelloWorldWS?wsdl来查看wsdl文档。 总结在SSH中添加CXF,关键性的两步:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |