apache CXF入门案例

本文介绍了Apache CXF的基本概念,并通过详细步骤展示了如何进行服务器端和客户端的开发。首先,创建web项目并导入CXF所需jar包,接着在web.xml配置CXFServlet,然后在cxf.xml中注册服务。接着,定义接口和实现类,并发布服务。客户端则使用wsimport命令生成本地代码,复制接口文件并配置服务。最后,测试调用服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.apache CXF简介:

Apache CXF是开源的,CXF是两个项目的结合:由IONA技术公司(现在是Progress的一部分)开发的Celtix和由Codehaus主持的团队开发的XFire,合并是由人们在Apache软件基金会共同完成的。CXF的名字来源于"Celtix"和"XFire"的首字母。是WebService的一个框架.

二.案例:服务器端开发

1.创建web项目,导入cxf必jar须包https://pan.baidu.com/s/1cPC6KbO_DE25oZIWEy9JUg

2.在web.xml中配置CXF框架提供的一个Servlet:

<!-- 配置CXF框架提供的Servlet -->
  <servlet>
      <servlet-name>cxf</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <!-- 通过初始化参数指定CXF框架的配置文件位置 -->
      <init-param>
          <param-name>config-location</param-name>
          <param-value>classpath:cxf.xml</param-value>
      </init-param>
  </servlet>
  <servlet-mapping>
      <servlet-name>cxf</servlet-name>
      <url-pattern>/service/*</url-pattern>
  </servlet-mapping>

3.在类路径下提供cxf.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"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                    http://www.springframework.org/schema/beans/spring-beans.xsd
                    http://cxf.apache.org/bindings/soap
                    http://cxf.apache.org/schemas/configuration/soap.xsd
                    http://cxf.apache.org/jaxws
                    http://cxf.apache.org/schemas/jaxws.xsd">
    <!-- 引入CXF Bean定义如下,早期的版本中使用 -->
    <import resource="classpath:META-INF/cxf/cxf.xml" />
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

    <bean id="helloService" class="com.wenhao.service.SayHelloImpl"/>
    
    <!-- 注册服务 -->
    <jaxws:server id="myService" address="/cxfService">
        <jaxws:serviceBean>
            <ref bean="helloService"/>
        </jaxws:serviceBean>
    </jaxws:server>
</beans>

 4.开发一个接口和实现类

@WebService
public interface SayHello {
    public String sayWorld(String name);
}

public class SayHelloImpl implements SayHello {
    @Override
    public String sayWorld(String name) {
        System.out.println("cxf框架服务端调用了!!!!");
        return "你好" + name;
    }
}

5.在cxf.xml中注册服务

 6.运行,发布服务端代码

在浏览器中输入          http://localhost/CXFService/service/cxfService?wsdl        生成wsdl以供客户端调用

 三:客户端开发

1.使用jdk提供的wsimport命令生成本地代码完成调用

在cmd窗口下输入

生成的接口文件将会在当前目录下,

2.将接口文件复制到项目中

将生成的接口文件复制到项目中,并在applicationContext.xml文件中注册服务

<!-- 注册服务 -->
    <jaxws:client id="myClient" address="http://localhost/CXFService/service/cxfService"
                     serviceClass="com.wenhao.service.SayHello">
    </jaxws:client>

测试调用

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值