前面介绍了WebService与CXF的使用,项目中我们经常用到Spring,这里介绍CXF与Spring整合
步骤
1、创建一个Maven Web项目,可以参照:【Maven】Eclipse 使用Maven创建Java Web项目
2、添加spring依赖和cxf依赖的jar包,在pom文件中设置依赖即可
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 3 <modelVersion>4.0.0</modelVersion> 4 <groupId>test.hd</groupId> 5 <artifactId>test-webservice-cxf-spring</artifactId> 6 <packaging>war</packaging> 7 <version>0.0.1-SNAPSHOT</version> 8 <name>test-webservice-cxf-spring Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <!-- 定义maven变量 --> 11 <properties> 12 <!-- spring --> 13 <spring.version>4.0.0.RELEASE</spring.version> 14 15 <!-- log --> 16 <commons-logging.version>1.1.3</commons-logging.version> 17 18 <!-- CXF --> 19 <cxf-version>3.1.5</cxf-version> 20 21 <!-- Servlet --> 22 <servlet.version>3.0.1</servlet.version> 23 <jsp-api.version>2.2</jsp-api.version> 24 25 <!-- jstl --> 26 <jstl.version>1.2</jstl.version> 27 <standard.version>1.1.2</standard.version> 28 29 <!-- test --> 30 <junit.version>3.8.1</junit.version> 31 32 <!-- jdk --> 33 <jdk.version>1.7</jdk.version> 34 <maven.compiler.plugin.version>2.3.2</maven.compiler.plugin.version> 35 </properties> 36 37 38 <dependencies> 39 40 <dependency> 41 <groupId>org.springframework</groupId> 42 <artifactId>spring-core</artifactId> 43 <version>${spring.version}</version> 44 </dependency> 45 46 <dependency> 47 <groupId>org.springframework</groupId> 48 <artifactId>spring-beans</artifactId> 49 <version>${spring.version}</version> 50 </dependency> 51 52 <dependency> 53 <groupId>org.springframework</groupId> 54 <artifactId>spring-context</artifactId> 55 <version>${spring.version}</version> 56 </dependency> 57 58 <dependency> 59 <groupId>org.springframework</groupId> 60 <artifactId>spring-jdbc</artifactId> 61 <version>${spring.version}</version> 62 </dependency> 63 64 65 <dependency> 66 <groupId>org.springframework</groupId> 67 <artifactId>spring-expression</artifactId> 68 <version>${spring.version}</version> 69 </dependency> 70 71 <dependency> 72 <groupId>org.springframework</groupId> 73 <artifactId>spring-web</artifactId> 74 <version>${spring.version}</version> 75 </dependency> 76 77 <dependency> 78 <groupId>org.springframework</groupId> 79 <artifactId>spring-webmvc</artifactId> 80 <version>${spring.version}</version> 81 </dependency> 82 83 <dependency> 84 <groupId>org.springframework</groupId> 85 <artifactId>spring-tx</artifactId> 86 <version>${spring.version}</version> 87 </dependency> 88 89 <!-- CXF --> 90 <dependency> 91 <groupId>org.apache.cxf</groupId> 92 <artifactId>cxf-core</artifactId> 93 <version>${cxf-version}</version> 94 </dependency> 95 <dependency> 96 <groupId>org.apache.cxf</groupId> 97 <artifactId>cxf-rt-frontend-jaxws</artifactId> 98 <version>${cxf-version}</version> 99 </dependency> 100 <dependency> 101 <groupId>org.apache.cxf</groupId> 102 <artifactId>cxf-rt-transports-http</artifactId> 103 <version>${cxf-version}</version> 104 </dependency> 105 106 107 <!-- Servlet --> 108 <dependency> 109 <groupId>javax.servlet</groupId> 110 <artifactId>javax.servlet-api</artifactId> 111 <version>${servlet.version}</version> 112 <scope>provided</scope> 113 </dependency> 114 <dependency> 115 <groupId>javax.servlet.jsp</groupId> 116 <artifactId>jsp-api</artifactId> 117 <version>${jsp-api.version}</version> 118 <scope>provided</scope> 119 </dependency> 120 121 <!-- jstl --> 122 <dependency> 123 <groupId>javax.servlet</groupId> 124 <artifactId>jstl</artifactId> 125 <version>${jstl.version}</version> 126 </dependency> 127 128 <dependency> 129 <groupId>taglibs</groupId> 130 <artifactId>standard</artifactId> 131 <version>${standard.version}</version> 132 </dependency> 133 134 <!-- test --> 135 <dependency> 136 <groupId>junit</groupId> 137 <artifactId>junit</artifactId> 138 <version>${junit.version}</version> 139 <scope>test</scope> 140 </dependency> 141 142 </dependencies> 143 144 145 <build> 146 <plugins> 147 <!-- define the project compile level --> 148 <plugin> 149 <groupId>org.apache.maven.plugins</groupId> 150 <artifactId>maven-compiler-plugin</artifactId> 151 <version>${maven.compiler.plugin.version}</version> 152 <configuration> 153 <source>${jdk.version}</source> 154 <target>${jdk.version}</target> 155 </configuration> 156 </plugin> 157 </plugins> 158 <finalName>test-webservice-cxf-spring</finalName> 159 </build> 160 161 </project>
3、新建一个接口,使用@WebService(SEI和SEI的实现类)注解标注接口,使用@WebMethod(SEI中的所有方法)注解标注接口中定义的所有方法,如下:
1 package com.test.ws; 2 3 import javax.jws.WebMethod; 4 import javax.jws.WebService; 5 6 /** 7 * 定义SEI(WebService EndPoint Interface)终端 8 * @author H__D 9 * @date 2017年7月28日 上午11:35:34 10 * 11 */ 12 //使用@WebService注解标注WebServiceI接口 13 @WebService 14 public interface HelloWS { 15 16 //使用@WebMethod注解标注WebServiceI接口中的方法 17 @WebMethod 18 public String sayHello(String name); 19 20 }
4、编写一个接口实现类,使用@WebService注解标注实现类,如下:
1 package com.test.ws; 2 3 import javax.jws.WebService; 4 5 6 /** 7 * SEI的具体实现 8 * @author H__D 9 * @date 2017年7月28日 上午11:37:43 10 * 11 */ 12 //使用@WebService注解标注 13 @WebService 14 public class HelloWSImpl implements HelloWS{ 15 16 @Override 17 public String sayHello(String name) { 18 System.out.println("WebService sayHello : " + name); 19 return "Hello : " + name; 20 } 21 }
5、编辑web.xml文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 5 id="WebApp_ID" version="3.0"> 6 <display-name>test-webservice-cxf-spring</display-name> 7 8 <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 --> 9 <context-param> 10 <param-name>contextConfigLocation</param-name> 11 <param-value>classpath:spring/spring-context.xml</param-value> 12 </context-param> 13 14 <!-- Spring配置 --> 15 <listener> 16 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 17 </listener> 18 19 <!--cxfservlet的作用就是解析webservice请求 --> 20 <servlet> 21 <servlet-name>cxf</servlet-name> 22 <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> 23 <load-on-startup>1</load-on-startup> 24 </servlet> 25 <!-- 所有前缀为ws的请求都被解析为webservice请求 --> 26 <servlet-mapping> 27 <servlet-name>cxf</servlet-name> 28 <url-pattern>/ws/*</url-pattern> 29 </servlet-mapping> 30 31 <welcome-file-list> 32 <welcome-file>index.jsp</welcome-file> 33 </welcome-file-list> 34 </web-app>
6、在src/main/resources中新建一个文件夹spring,在spring文件夹中新建一个spring-content.xml的配置文件和一个cxf的webservice配置文件(cxf-beans.xml),进行相关配置
spring-content.xml
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" xmlns:mvc="http://www.springframework.org/schema/mvc" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5 xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 7 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 9 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd 10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 11 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> 12 13 14 <!-- 注解注册 --> 15 <context:annotation-config /> 16 17 <context:component-scan base-package="com.test" /> 18 19 <!-- 引入cxf的webservice配置文件 --> 20 <import resource="classpath:spring/cxf-beans.xml" /> 21 22 </beans>
cxf-beans.xml
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" xmlns:jaxws="http://cxf.apache.org/jaxws" 4 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"> 5 6 7 <!-- 引cxf的一些核心配置 --> 8 <import resource="classpath:META-INF/cxf/cxf.xml" /> 9 <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> 10 11 12 <!-- 定义webservice服务,相当于发布 --> 13 <jaxws:server id="HelloWS" address="/helloWS" 14 serviceClass="com.test.ws.HelloWS"> 15 <!--发布服务类 --> 16 <jaxws:serviceBean> 17 <bean class="com.test.ws.HelloWSImpl"></bean> 18 </jaxws:serviceBean> 19 20 <!-- 输入拦截器 --> 21 <jaxws:inInterceptors> 22 <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean> 23 </jaxws:inInterceptors> 24 25 <!-- 输出拦截器 --> 26 <jaxws:outInterceptors> 27 <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean> 28 </jaxws:outInterceptors> 29 </jaxws:server> 30 31 </beans>
7、搭建完成,如下图
8、发布到tomcat中,并启动tomcat
9、使用浏览器访问获取wsdl文件,或者进行webservice调用都行