首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Spring 整合 Apache CXF 實戰教程:快速構建 Web Service 服務端與客戶端

     編輯:Java 綜合教學

Spring 整合 Apache CXF 實戰教程

近期專案需要使用 Web Service,經過研究 Spring 整合 CXF 的官方文檔(文檔說明清晰,建議直接閱讀官方英文文檔),整理出一份完整的 Demo 供參考。本教程採用 Maven + Spring + CXF + IntelliJ IDEA 技術棧,帶你從零搭建一個可運行的 Web Service 應用。

一、專案結構總覽

專案採用標準 Maven 目錄結構,核心模塊分為服務端與客戶端兩部分。以下為專案結構圖:

專案結構圖

二、服務端開發步驟

1. 定義服務接口

首先創建 HelloWorld 接口,使用 @WebService 標註為 Web Service 端點:

package cn.starlin.springcxf;

import javax.jws.WebService;

/**
 * 定義服務接口
 */
@WebService
public interface HelloWorld {
    String sayHello(String str);
}

2. 實現服務接口

創建實現類 HelloWorldImpl,通過 endpointInterface 指定對應接口:

package cn.starlin.springcxf;

import javax.jws.WebService;

/**
 * 服務接口實現類
 */
@WebService(endpointInterface = "cn.starlin.springcxf.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
    @Override
    public String sayHello(String str) {
        System.out.println("sayHello called");
        return "Hello " + str;
    }
}

3. 配置 CXF Spring 整合文件

在 resources 目錄下創建 cxf-servlet.xml,聲明 Service Bean 並發布端點:

<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="https://cxf.apache.org/jaxws"
       xsi:schemaLocation="
           https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
           https://cxf.apache.org/jaxws https://cxf.apache.org/schemas/jaxws.xsd">

    <!-- 由 Spring 管理服務實現 Bean -->
    <bean id="hello" class="cn.starlin.springcxf.HelloWorldImpl" />

    <!-- 發布 Web Service 端點,地址為 /HelloWorld -->
    <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" />
</beans>

4. 配置 web.xml 載入 CXF Servlet

<web-app xmlns="https://java.sun.com/xml/ns/javaee"
         xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
         version="2.5"
         xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>cxf</display-name>
    <servlet>
        <description>Apache CXF Endpoint</description>
        <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>
    </servlet>
    <servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>
</web-app>

5. 驗證服務端啟動

啟動 Tomcat,訪問 https://localhost:8080/SpringCXF/,若出現 CXF 服務列表頁面,表示服務端發布成功:

CXF 服務列表頁面

三、客戶端開發步驟

1. 客戶端 Spring 配置

創建 client-beans.xml,使用 JaxWsProxyFactoryBean 創建代理:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="https://www.springframework.org/schema/beans"
       xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
       xmlns:jaxws="https://cxf.apache.org/jaxws"
       xsi:schemaLocation="
           https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
           https://cxf.apache.org/jaxws https://cxf.apache.org/schema/jaxws.xsd">

    <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
        <property name="serviceClass" value="cn.starlin.springcxf.HelloWorld"/>
        <property name="address" value="https://localhost:8080/SpringCXF/HelloWorld"/>
    </bean>

    <bean id="client" class="cn.starlin.springcxf.HelloWorld"
          factory-bean="clientFactory" factory-method="create"/>
</beans>

2. 編寫客戶端調用代碼

package cn.starlin.springcxf;

import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 客戶端測試類
 */
public class HelloWorldClient {
    public static void main(String[] args) throws Exception {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("client-beans.xml");
        HelloWorld client = (HelloWorld) context.getBean("client");
        String response = client.sayHello("starlin");
        System.out.println("Response: " + response);
        System.exit(0);
    }
}

3. 運行驗證

運行 HelloWorldClient,控制台輸出 Response: Hello starlin 即表示調用成功。服務端控制台同步打印 sayHello called。

客戶端運行結果

服務端控制台輸出


至此,Spring 整合 CXF 的 Web Service 開發全流程完成。掌握此模式,可快速構建企業級 SOAP 服務。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved