WebService小白学习 之 Spring整合CXF,添加拦截器 (7)

上篇 WebService小白学习 之 CXF添加拦截器,自定义拦截器

这篇为Spring整合CXF开发。

实现过程:

1、创建 mavenweb空项目 -服务端项目。

2、在pom.xml添加spring和cxf依赖,使用了spring,使用tomcat发布服务,所以依赖最后一个不需要jetty

		<!-- 添加Spring支持 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>


		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.1.7.RELEASE</version>
		</dependency>

		<!-- 添加cxf支持 -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-core</artifactId>
			<version>3.1.5</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.1.5</version>
		</dependency>

		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>3.1.5</version>
		</dependency>

3、添加配置文件applicationContext.xml到src/main/resources目录下

<?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:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jee="http://www.springframework.org/schema/jee"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="    
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd  
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd  
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.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-servlet.xml"/>
    
    <!-- 自动扫描 -->
	<context:component-scan base-package="com.gx.webservice" />
	
	<!-- 定义服务提供者,访问路径拼接  -->
	<jaxws:endpoint implementor="#helloWorld" address="/HelloWorld">
		<!-- 添加in拦截器 -->
	    <jaxws:inInterceptors>
	         <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
	         <bean class="com.gx.interceptor.MyInInterceptor"/>
	    </jaxws:inInterceptors>
	    <!-- 添加out拦截器 -->
	    <jaxws:outInterceptors>
	         <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
	    </jaxws:outInterceptors>
	</jaxws:endpoint>
	
</beans>

4、复制以前服务端项目的包到当前项目src/main/java,server包删除了,因为等下使用tomcat发布服务,目录:

5、在HelloWorldImpl.java添加@Component("helloWorld")注解

6、配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
	<display-name>WebserviceSpring</display-name>
	<welcome-file-list>
	  <welcome-file>index.html</welcome-file>
	  <welcome-file>index.htm</welcome-file>
	  <welcome-file>index.jsp</welcome-file>
	  <welcome-file>default.html</welcome-file>
	  <welcome-file>default.htm</welcome-file>
	  <welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
  
  	<!-- Spring配置文件 -->
	<context-param>
	    <param-name>contextConfigLocation</param-name>
	    <param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	 
	<!-- Spring监听器 -->
	<listener>
	    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<servlet>  
		<servlet-name>CXFServlet</servlet-name>  
		<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
	</servlet>  
	 
	<servlet-mapping>  
	    <servlet-name>CXFServlet</servlet-name>  
	    <url-pattern>/webservice/*</url-pattern>  
	</servlet-mapping>
  
</web-app>

7、添加服务端项目到tomcat中,运行

8、浏览器测试,输入地址http://localhost:8080/WSSpringServer/webservice/HelloWorld?wsdl

测试结果和以前一样:

9、创建 mavenweb空项目 -客户端项目

10、在pom.xml添加下面的,和以前未整合spring客户端一样

	<dependencies>
		<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-core -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-core</artifactId>
			<version>3.2.5</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>3.2.5</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http-jetty -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http-jetty</artifactId>
			<version>3.2.5</version>
		</dependency>

	</dependencies>

11、复制未整合客户端项目以下文件到当前项目

12、使用cxf工具重新文件,注意启动tomcat

生成成功,刷新项目,会发现和以前有点不一样

13、修改

HelloWorldImplService helloWorldService = new HelloWorldImplService(); //改动
HelloWorld helloworld =  helloWorldService.getHelloWorldImplPort(); //改动

运行client.java结果

ok。

webservice的学习部分到此,其他待续。

demo下载:

webservice基础学习

webservice之Spring整合CXF

 

posted @ 2018-07-31 08:43  夯实点  阅读(219)  评论(0编辑  收藏  举报