简介

Apache CXF = Celtix + XFire,开始叫 Apache CeltiXfire,后来更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF 已经是一个正式的 Apache 顶级项目。

简单使用

编写服务端

1、新建 maven web 工程作为服务端,引入如下依赖:

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.zze</groupId>
  <artifactId>cxf_server</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-frontend-jaxws</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.cxf</groupId>
      <artifactId>cxf-rt-transports-http</artifactId>
      <version>3.0.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.2.4.RELEASE</version>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <!--使用 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>
</project>

pom.xml

2、新建服务类接口及其实现类:

package com.zze.webservice;

import javax.jws.WebService;
@WebService
public interface ITestService {
    String hello();
}

com.zze.webservice.ITestService

package com.zze.webservice.impl;

import com.zze.webservice.ITestService;

public class TestService implements ITestService {
    public String hello() {
        return "hello CXF";
    }
}

com.zze.webservice.impl.TestService

3、在 Spring 配置文件中发布服务:

<?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">
    <!--调用的类-->
    <bean id="testService" class="com.zze.webservice.impl.TestService"></bean>
    <!--发布服务-->
    <jaxws:server address="/testService">
        <jaxws:serviceBean>
            <ref bean="testService"/>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>

applicationContext_cxf.xml

4、配置 Spring 监听器及 CXF 过滤器:

<!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>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext_cxf.xml</param-value>
    </context-param>
    <filter>
        <filter-name>cxf</filter-name>
        <filter-class>org.apache.cxf.transport.servlet.CXFServlet</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>cxf</filter-name>
        <url-pattern>/ws/*</url-pattern>
    </filter-mapping>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

WEB-INF/web.xml

5、启动程序(服务随之发布),测试服务是否发布成功,发布成功则如下:

编写客户端

1、新建 maven java 工程作为客户端,引入如下依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zze</groupId>
    <artifactId>cxf_client</artifactId>
    <version>1.0-SNAPSHOT</version>

   <dependencies>
       <dependency>
           <groupId>org.apache.cxf</groupId>
           <artifactId>cxf-rt-frontend-jaxws</artifactId>
           <version>3.0.1</version>
       </dependency>
       <dependency>
           <groupId>org.apache.cxf</groupId>
           <artifactId>cxf-rt-transports-http</artifactId>
           <version>3.0.1</version>
       </dependency>
       <dependency>
           <groupId>org.springframework</groupId>
           <artifactId>spring-context</artifactId>
           <version>4.2.4.RELEASE</version>
       </dependency>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
           <scope>test</scope>
       </dependency>
   </dependencies>
</project>

pom.xml

2、cmd 进入 classpath 目录下,执行如下命令生成客户端代码:

wsimport -s . http://localhost:8080/ws/testService?wsdl

3、在 Spring 配置文件中配置调用服务的客户端实例:

<?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">
    <jaxws:client id="testClient" address="http://localhost:8080/ws/testService?wsdl"
                  serviceClass="com.zze.webservice.impl.ITestService"/>
</beans>

applicationContext_cxf.xml

4、编写代码调用服务端,如下输出则调用成功:

package com.zze.webservice.test;

import com.zze.webservice.impl.ITestService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CXFTest{
    @Test
    public void test1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext_cxf.xml");
        ITestService testClient = (ITestService)applicationContext.getBean("testClient");
        System.out.println(testClient.hello());
    }
}

com.zze.webservice.test.CXFTest

java之Spring集成CXF简单调用的更多相关文章

  1. Spring集成CXF发布WebService并在客户端调用

    Spring集成CXF发布WebService 1.导入jar包 因为官方下载的包里面有其他版本的sprring包,全导入会产生版本冲突,所以去掉spring的部分,然后在项目根目录下新建了一个CXF ...

  2. 脱离spring集成cxf(基于nutz框架)

    什么是webService WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 理论资料: http://blog.csdn.net/wooshn/article/details/8 ...

  3. Spring集成CXF获取HttpServletRequest,HttpServletResponse

    最近的项目中,在Spring继承CXF中要用到request来获取IP,所以先要获取到HttpServletRequest对象,具体方法如下: 1.配置文件: <jaxrs:server id= ...

  4. spring集成cxf实现webservice接口功能

    由于cxf的web项目已经集成了Spring,所以cxf的服务类都是在spring的配置文件中完成的.以下是步骤:第一步:建立一个web项目.第二步:准备所有jar包.将cxf_home\lib项目下 ...

  5. webService学习之路(三):springMVC集成CXF后调用已知的wsdl接口

    webService学习之路一:讲解了通过传统方式怎么发布及调用webservice webService学习之路二:讲解了SpringMVC和CXF的集成及快速发布webservice 本篇文章将讲 ...

  6. springMVC集成CXF后调用已知的wsdl接口

    本文转载自:https://www.cnblogs.com/xiaochangwei/p/5400303.html 本篇文章将讲解SpringMVC+CXF环境下,怎么调用其他系统通过webServi ...

  7. spring集成jedis简单实例

    jedis是redis的java客户端,spring将redis连接池作为一个bean配置. redis连接池分为两种,一种是“redis.clients.jedis.ShardedJedisPool ...

  8. spring集成mongodb简单使用和测试方式

    @EnableMongoRepositories @ComponentScan(basePackages = "cn.example") @Configuration public ...

  9. spring 集成redis客户端jedis(java)

    spring集成jedis简单实例   jedis是redis的java客户端,spring将redis连接池作为一个bean配置. “redis.clients.jedis.JedisPool”,这 ...

随机推荐

  1. LZW算法PHP实现方法 lzw_decompress php

    LZW算法PHP实现方法 lzw_decompress php 博客分类: Php / Pear / Mysql / Node.js   LZW算法简介 字符串和编码的对应关系是在压缩过程中动态生成的 ...

  2. python线程池(threadpool)模块使用笔记 .python 线程池使用推荐

    一.安装与简介 pip install threadpool pool = ThreadPool(poolsize) requests = makeRequests(some_callable, li ...

  3. Syncfusion SfDataGrid 导出Excel

    var options = new ExcelExportingOptions { ExcelVersion = ExcelVersion.Excel2013, }; //不需要导出的字段 optio ...

  4. Laravel 执行流程(一)之自动加载

    定位 从 public/index.php 定位到 bootstrap/autoload.php 从 bootstrap/autoload.php 定位到 vendor/autoload.php 从 ...

  5. 【OCP、OCM、高可用等】小麦苗课堂网络班招生简章(从入门到专家)--课程大纲

    [OCP.OCM.高可用等]小麦苗课堂网络班招生简章(从入门到专家)--课程大纲 小麦苗信息 我的个人信息 网名:小麦苗 QQ:646634621 QQ群:618766405 我的博客:http:// ...

  6. 智能聊天机器人——基于RASA搭建

    前言: 最近了解了一下Rasa,阅读了一下官方文档,初步搭建了一个聊天机器人. 官方文档:https://rasa.com/docs/ 搭建的chatbot项目地址: https://github.c ...

  7. Centos VMware 克隆后 网络配置

    第一步:生产新的网卡地址,启动系统. 第二步:修改主机名(注:此处根据个人需要,不修改也行,此处我是用于搭建集群,修改主机名做区分) 执行命令:vi /etc/sysconfig/network 修改 ...

  8. Day8 函数指针做函数参数

    课堂笔记 课程回顾         多态 virtual关键字 纯虚函数 virtual func() = 0;         提前布局vptr指针 面向接口编程 延迟绑定 多态的析构函数的虚函数. ...

  9. Qt编写自定义控件3-速度仪表盘

    前言 速度仪表盘,写作之初的本意是用来展示当前测试的网速用的,三色圆环+数码管显示当前速度,Qt自带了数码管控件QLCDNumber,直接集成即可,同时还带有动画功能,其实也可以用在汽车+工业领域等, ...

  10. C# 客户端篇之实现Restful Client开发(RestSharp帮助类)

    上篇文章<C# 服务端篇之实现RestFul Service开发(简单实用)>讲解到,如果开发一个简单的Restful风格的Service,也提到了简单创建一个Restful Client ...