【WebService】WebService之CXF和Spring整合(六)

时间:2023-03-09 05:06:17
【WebService】WebService之CXF和Spring整合(六)

  前面介绍了WebService与CXF的使用,项目中我们经常用到Spring,这里介绍CXF与Spring整合

步骤

  1、创建一个Maven Web项目,可以参照:【Maven】Eclipse 使用Maven创建Java Web项目

  2、添加spring依赖和cxf依赖的jar包,在pom文件中设置依赖即可

 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test.hd</groupId>
<artifactId>test-webservice-cxf-spring</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>test-webservice-cxf-spring Maven Webapp</name>
<url>http://maven.apache.org</url>
<!-- 定义maven变量 -->
<properties>
<!-- spring -->
<spring.version>4.0.0.RELEASE</spring.version> <!-- log -->
<commons-logging.version>1.1.3</commons-logging.version> <!-- CXF -->
<cxf-version>3.1.5</cxf-version> <!-- Servlet -->
<servlet.version>3.0.1</servlet.version>
<jsp-api.version>2.2</jsp-api.version> <!-- jstl -->
<jstl.version>1.2</jstl.version>
<standard.version>1.1.2</standard.version> <!-- test -->
<junit.version>3.8.1</junit.version> <!-- jdk -->
<jdk.version>1.7</jdk.version>
<maven.compiler.plugin.version>2.3.2</maven.compiler.plugin.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency> <!-- CXF -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>${cxf-version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf-version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf-version}</version>
</dependency> <!-- Servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>${jsp-api.version}</version>
<scope>provided</scope>
</dependency> <!-- jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency> <dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${standard.version}</version>
</dependency> <!-- test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency> </dependencies> <build>
<plugins>
<!-- define the project compile level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
<finalName>test-webservice-cxf-spring</finalName>
</build> </project>

  

  3、新建一个接口,使用@WebService(SEI和SEI的实现类)注解标注接口,使用@WebMethod(SEI中的所有方法)注解标注接口中定义的所有方法,如下:

 package com.test.ws;

 import javax.jws.WebMethod;
import javax.jws.WebService; /**
* 定义SEI(WebService EndPoint Interface)终端
* @author H__D
* @date 2017年7月28日 上午11:35:34
*
*/
//使用@WebService注解标注WebServiceI接口
@WebService
public interface HelloWS { //使用@WebMethod注解标注WebServiceI接口中的方法
@WebMethod
public String sayHello(String name); }

  4、编写一个接口实现类,使用@WebService注解标注实现类,如下:

 package com.test.ws;

 import javax.jws.WebService;

 /**
* SEI的具体实现
* @author H__D
* @date 2017年7月28日 上午11:37:43
*
*/
//使用@WebService注解标注
@WebService
public class HelloWSImpl implements HelloWS{ @Override
public String sayHello(String name) {
System.out.println("WebService sayHello : " + name);
return "Hello : " + name;
}
}

  5、编辑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>test-webservice-cxf-spring</display-name> <!-- 指定Spring Bean的配置文件所在目录。默认配置在WEB-INF目录下 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-context.xml</param-value>
</context-param> <!-- Spring配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> <!--cxfservlet的作用就是解析webservice请求 -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 所有前缀为ws的请求都被解析为webservice请求 -->
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping> <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

  6、在src/main/resources中新建一个文件夹spring,在spring文件夹中新建一个spring-content.xml的配置文件和一个cxf的webservice配置文件(cxf-beans.xml),进行相关配置
    【WebService】WebService之CXF和Spring整合(六)
    spring-content.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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 注解注册 -->
<context:annotation-config /> <context:component-scan base-package="com.test" /> <!-- 引入cxf的webservice配置文件 -->
<import resource="classpath:spring/cxf-beans.xml" /> </beans>

    cxf-beans.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"
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"> <!-- 引cxf的一些核心配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!-- 定义webservice服务,相当于发布 -->
<jaxws:server id="HelloWS" address="/helloWS"
serviceClass="com.test.ws.HelloWS">
<!--发布服务类 -->
<jaxws:serviceBean>
<bean class="com.test.ws.HelloWSImpl"></bean>
</jaxws:serviceBean> <!-- 输入拦截器 -->
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors> <!-- 输出拦截器 -->
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
</jaxws:server> </beans>

  7、搭建完成,如下图
       【WebService】WebService之CXF和Spring整合(六)

  8、发布到tomcat中,并启动tomcat
      【WebService】WebService之CXF和Spring整合(六)

  9、使用浏览器访问获取wsdl文件,或者进行webservice调用都行
      【WebService】WebService之CXF和Spring整合(六)