cxf整合Spring实现webservice调用实践

 更新时间:2026年04月27日 14:37:44   作者:wmxz520  
文章介绍了使用CXF整合Spring实现WebService的过程,包括pom文件配置、web.xml和applicationContext.xml文件配置、接口定义与实现、项目运行及测试,通过访问wsdl地址验证服务可用性,最后通过客户端测试类验证调用成功

cxf整合Spring实现webservice

server端

pom文件内容

<dependencies>
    <!-- CXF WS开发  -->
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.2</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
          <encoding>UTF-8</encoding>
          <showWarnings>true</showWarnings>
        </configuration>
      </plugin>
      <!-- 运行tomcat7方法:tomcat7:run -->
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <!-- 指定端口 -->
          <port>8080</port>
          <!-- 请求路径 -->
          <path>/</path>
        </configuration>
      </plugin>
    </plugins>
  </build>

web.xml文件里的内容

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <display-name>Archetype Created Web Application</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!--webservice服务端,发布服务需要配置CXFSrvlet-->
  <!--这里配置的servlet路径,为最终服务路径的一部分-->
  <!--服务访问路径:http://localhost:8080/web.xml配置路径/spring配置的路
径 -->
  <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>/ws/*</url-pattern>
  </servlet-mapping>
</web-app>

applicationContext.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:cxf="http://cxf.apache.org/core"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core
        http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd">
    <!--
    1. 服务地址
    2. 服务bean
    完整服务地址:
    http://localhost:8080/ws/userService
    Spring整合ApacheCXF发布jaxws服务
	-->
    <jaxws:server address="/userService">
        <jaxws:serviceBean>
            <bean class="com.sks.service.impl.UserServiceImpl"/>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>

接口

@WebService
public interface UserService {

    public String sayHello(String name, int age);
}

接口实现类

public class UserServiceImpl implements UserService {

    @Override
    public String sayHello(String name, int age) {
        return "hello " + name + " " + age;
    }
}

最后点击如图所示的按钮运行项目。

在浏览器中输入http://localhost:8080/ws/userService?wsdl,访问wsdl说明书,可以看到以下内容:

client端

项目目录结构,web.xml文件中的内容不需要改动

applicationContext.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:cxf="http://cxf.apache.org/core"
       xmlns:jaxws="http://cxf.apache.org/jaxws"
       xmlns:jaxrs="http://cxf.apache.org/jaxrs"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core
        http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd">
    <!--Spring整合ApacheCXF,客户端配置
    关键点:
    通过Spring整合ApacheCXF,创建客户端的代理对象,远程访问服务端。
    jaxws:client
    id 应用中注入的接口的代理对象的名称
    address 服务端访问地址
    serviceClass 指定接口路径,会根据该类型生成代理对象
    -->
    <jaxws:client id="userService" address="http://localhost:8080/ws/userService" serviceClass="com.sks.service.UserService">
    </jaxws:client>
</beans>

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test1 {

    @Resource
    private UserService userService;

    @Test
    public void test1() {
        System.out.println(userService);
        System.out.println(userService.getClass());
        //远程服务调用接口
        String context = userService.sayHello("球球",19);
        System.out.println(context);
    }
}

点击运行以后控制台打印输出结果如下,这说明我们的调用成功了。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Spring Security动态权限的实现方法详解

    Spring Security动态权限的实现方法详解

    这篇文章主要和小伙伴们简单介绍下 Spring Security 中的动态权限方案,以便于小伙伴们更好的理解 TienChin 项目中的权限方案,感兴趣的可以了解一下
    2022-06-06
  • Java实时信号处理的五种方式

    Java实时信号处理的五种方式

    实时信号处理还在卡顿?这和用老式打字机写小说有什么区别,Java与实时信号处理系统的集成就像给系统装了个"魔法加速器",今天我们就用Java代码+实战技巧,打造一套"实时信号处理的魔法工具箱",让你的系统比"咖啡机出水"还流畅,需要的朋友可以参考下
    2025-06-06
  • SpringBoot+MyBatis-Plus实现分页功能

    SpringBoot+MyBatis-Plus实现分页功能

    在SpringBoot项目中,结合MyBatis-Plus(简称MP)可以非常方便地实现分页功能,MP为开发者提供了分页插件PaginationInterceptor,只需简单配置即可使用,本文给大家介绍了SpringBoot+MyBatis-Plus实现分页功能,文中通过代码示例给大家介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • System.currentTimeMillis()计算方式与时间的单位转换详解

    System.currentTimeMillis()计算方式与时间的单位转换详解

    这篇文章主要介绍了System.currentTimeMillis()计算方式与时间的单位转换详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • Java递归方法求5!的实现代码

    Java递归方法求5!的实现代码

    这篇文章主要介绍了Java递归方法求5!的实现代码,需要的朋友可以参考下
    2017-02-02
  • 基于Java实现一个高效可伸缩的计算结果缓存

    基于Java实现一个高效可伸缩的计算结果缓存

    这篇文章将通过对一个计算结果缓存的设计迭代介绍,分析每个版本的并发缺陷,并分析如何修复这些缺陷,最终完成一个高效可伸缩的计算结果缓存,感兴趣的小伙伴可以了解一下
    2023-06-06
  • 浅谈java中文本框和文本区

    浅谈java中文本框和文本区

    本文给大家介绍的是java中的文本框和文本区的概念和使用方法,以及简单的示例,十分实用,有需要的小伙伴可以参考下。
    2015-06-06
  • SpringMVC的源码解析

    SpringMVC的源码解析

    本文主要介绍了SpringMVC的源码解析。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-02-02
  • IntelliJ IDEA安装scala插件并创建scala工程的步骤详细教程

    IntelliJ IDEA安装scala插件并创建scala工程的步骤详细教程

    这篇文章主要介绍了IntelliJ IDEA安装scala插件并创建scala工程的步骤,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-07-07
  • 详解Java修饰符

    详解Java修饰符

    Java语言提供了很多修饰符,主要分为以下两类:访问修饰符;非访问修饰符。修饰符用来定义类、方法或者变量,通常放在语句的最前端。我们通过下面的例子来说明,下面就跟小编一起来看下吧
    2016-12-12

最新评论