CXF+Spring+Tomcat简明示例

本文介绍如何使用 Spring 和 CXF 框架来实现一个简单的 WebService 示例。包括定义接口、接口实现、配置服务端及客户端 XML 文件、web.xml 配置以及测试类的编写过程。

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

cxf就不多介绍了 ,直接上代码例子了:

1:接口类

package service;

import javax.jws.WebService;

/**
 * WebService接口
 * */
@WebService
public interface IHelloService {
    public  String sayHello( String username);
}

2:接口的实现类:

package service.impl;

import javax.jws.WebMethod;
import javax.jws.WebService;
import service.IHelloService;
@WebService(serviceName="HelloService",endpointInterface="service.IHelloService")
public class HelloServiceImpl implements IHelloService {
    @Override
    public String sayHello(String username) {
        // TODO Auto-generated method stub
        String name="hello"+username;
        return name;
    }
}

3:创建src/applicationContext-server.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">
    <!-- 手动的添加头信息:
          xmlns:jaxws="http://cxf.apache.org/jaxws"
          http://cxf.apache.org/jaxws
             http://cxf.apache.org/schemas/jaxws.xsd"
    -->
    <!--在META-INF创建文件夹cxf ,并在cxf里面建立 cxf.xml  cxf-extension-soap.xml cxf-servlet.xml (例如:META-INF/cxf/cxf.xml)
    文件里面的内容可以去 cxf-2.7.1.jar/META-INF/cxf/cxf.xml
    cxf-extension-soap.xml  cxf-servlet.xml  里面复制对应的内容-->
    <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="helloServiceImpl" class="service.impl.HelloServiceImpl" />
    <!-- 注意下面的address,这里的address的名称就是访问的WebService的name  访问路径 :项目路径 + address+?wsdl(http://127.0.0.1:8080/webService/helloService?wsdl) -->
    <jaxws:server id="helloService" serviceClass="service.IHelloService"
        address="/helloService">
        <jaxws:serviceBean>
            <!-- 要暴露的 bean 的引用 -->
            <ref bean="helloServiceImpl" />
        </jaxws:serviceBean>
    </jaxws:server>
</beans>
4:建立src/applicationContext-client.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">
    <!--/webService/
        ***注意***
        手动添加的内容:
        xmlns:jaxws="http://cxf.apache.org/jaxws"
        http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
     -->
     <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"/>

    <jaxws:client id="userWsClient" serviceClass="service.IHelloService"
        address="http://127.0.0.1:8080/webService/helloService"/>
</beans>

5:web.xml配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <!-- 加载Spring容器配置 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-server.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
    <servlet-name>CXFService</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>CXFService</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

6:测试类:

package service.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.IHelloService;
public class TestSpringCxf {

    @Test
    public void testSpringCxf(){
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
         IHelloService service = ctx.getBean("userWsClient", IHelloService.class);
         String name = service.sayHello("小辉");
         System.out.println("#############Client getUserByName##############"+name);
    }
}
第一篇博文不好请见谅


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值