用Maven搭建基于CXF框架的简单WebServiceDemo

本文介绍了如何在IDEA 14中使用Maven创建一个基于CXF框架的Web Service项目。首先新建名为CXFWebService的项目,包含服务端webService_server和客户端webService_client两个模块。接着引入相关依赖,然后创建服务接口及其实现,并通过jetty配置运行服务。客户端主要涉及Spring配置,通过cxf-client.xml配置文件和SpringClient类进行服务调用测试。最后,通过jetty启动服务,验证客户端能够成功调用服务端接口。

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

工具:IDEA 14

步骤一 新建项目

项目名CXFWebService
新建两个模块webService_server和webService_client
这里写图片描述

步骤二 引入依赖
<modules>
     <module>WebService_server</module>
     <module>WebService_Client</module>
</modules>

    <groupId>me.yulin.rao</groupId>
    <artifactId>CXFWebServices</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <properties>
        <cxf.version>2.7.5</cxf.version>
    </properties>
    <dependencies>
        <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>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>3.2.3.RELEASE</version>
        </dependency>
    </dependencies>
步骤四 创建服务端

编写暴露在外的服务接口

import javax.jws.WebService;
@WebService
public interface IhelloWorld {
    public String sayHi(String who);
}

编写接口的实现

import com.yoyo.Interface.IhelloWorld;
import javax.jws.WebService;
@WebService(endpointInterface = "com.yoyo.Interface.IhelloWorld")
public class HelloWorldImpl implements IhelloWorld {
    @Override
    public String sayHi(String who) {
        return "Hi,"+who;
    }
}

服务端用jetty起,所以先配置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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         id="WebApp_ID" version="2.5">
    <display-name>Archetype Created Web Application</display-name>

    <servlet>
        <display-name>cxf</display-name>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
        <init-param>
            <param-name>config-location</param-name>
            <param-value>WEB-INF/cxf-servlet.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>    

接下来就是配置cxf的servlet:在WEB-INF路径下新建cxf-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:simple="http://cxf.apache.org/simple"
       xmlns:soap="http://cxf.apache.org/bindings/soap"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd">

    <simple:server id="helloWorld" serviceClass="com.yoyo.Interface.IhelloWorld" address="http://localhost:8080/Interface/IhelloWorld?wsdl">
        <simple:serviceBean>
            <bean class="com.yoyo.Impl.HelloWorldImpl" />
        </simple:serviceBean>
    </simple:server>

</beans>

说明一下,这里的address可以自定义,如果在本地跑的话,只需要和后面客户端的cxf-client.xml中的address一致即可,但是server端的address末尾需要加上“?wsdl”。
并且在这里使用的是不用任何注解,完全依赖反射实现WS的编程模型”simple”,还有另外三种模型:Annotations,Dynamic Clients,JAX-WS。
另外,不要忘了webService_server模块自己的pom文件

步骤五 创建客户端

客户端主要是Spring配置,代码量较少
首先是cxf-client.xml配置(xml的名字随意)

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:simple="http://cxf.apache.org/simple"
       xmlns:soap="http://cxf.apache.org/bindings/soap"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd">

    <simple:client id="helloWorld"
                   serviceClass="com.yoyo.Interface.IhelloWorld" address="http://localhost:8080/Interface/IhelloWorld" />

</beans>

配置好之后,新增一个SpringClient类,测试一下能否调用服务端的服务

import com.yoyo.Interface.IhelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringClient {
    public static void main(String args[]) {
        ApplicationContext context = new ClassPathXmlApplicationContext("cxf-client.xml");
        IhelloWorld client =(IhelloWorld)context.getBean("helloWorld");
        System.out.println(client.sayHi("yoyo"));
    }
}
步骤六 配置好jetty测试一下

将jetty跑起来后,浏览器显示如下:
这里写图片描述

然后run我们刚编写的SpringClient
控制台显示如下:
这里写图片描述

到此demo结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值