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

Apache CXF 建立 Web Service 完整教學:從專案建立到部署測試

     編輯:Java 相關知識

使用 Apache CXF 快速建立 Web Service

Apache CXF 是新一代開源 Web Service 框架,支援 JAX-WS、JAX-RS 等標準,整合 Spring 架構,開發體驗優於舊版 Axis。本文透過完整範例示範從專案建立、服務實作、Spring 整合到客戶端測試的完整流程。

專案結構概覽

CXF 專案結構圖

採用標準 Maven/Web 專案結構,核心包含服務介面、實作類、Spring 設定檔與 web.xml。

核心代碼實作

1. 服務介面

package jp.co.apm.service;

public interface TestService {
    String sayHello();
}

2. 服務實作類

package jp.co.apm.service.impl;

import jp.co.apm.service.TestService;

public class TestServiceImpl implements TestService {
    @Override
    public String sayHello() {
        return "Hello, Shen Bin";
    }
}

3. Spring 整合設定 (cxf-servlet.xml)

<?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:simple="https://cxf.apache.org/simple"
    xmlns:soap="https://cxf.apache.org/bindings/soap"
    xsi:schemaLocation="
        https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.0.xsd
        https://cxf.apache.org/bindings/soap https://cxf.apache.org/schemas/configuration/soap.xsd
        https://cxf.apache.org/simple https://cxf.apache.org/schemas/simple.xsd">

    <simple:server id="testservice" serviceClass="jp.co.apm.service.TestService" address="/test">
        <simple:serviceBean>
            <bean class="jp.co.apm.service.impl.TestServiceImpl" />
        </simple:serviceBean>
    </simple:server>

</beans>

4. Web 部署描述檔 (web.xml)

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="https://java.sun.com/xml/ns/javaee"
    xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2.5.xsd">

    <display-name>APM</display-name>
    <description>APM</description>

    <servlet>
        <servlet-name>APM</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>APM</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

    <session-config>
        <session-timeout>60</session-timeout>
    </session-config>

</web-app>

5. 客戶端測試程式

package jp.co.apm.client;

import jp.co.apm.service.TestService;
import org.apache.cxf.frontend.ClientProxyFactoryBean;

public class TestServiceClient {
    public static void main(String[] args) {
        ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
        factory.setServiceClass(TestService.class);
        factory.setAddress("https://localhost:8080/APM_CXF/services/test");

        TestService service = (TestService) factory.create();
        System.out.println("服務回應: " + service.sayHello());
    }
}

部署與驗證步驟

  1. 啟動 Tomcat 伺服器
  2. 瀏覽器訪問:https://localhost:8080/APM_CXF/services/test?wsdl 驗證 WSDL 生成
  3. 執行 TestServiceClient.java 進行功能測試

CXF 核心優勢

  • 標準相容:完整支援 JAX-WS 2.x / JAX-RS 2.x 規範
  • Spring 深度整合:透過命名空間簡化設定,支援 AOP、事務管理
  • 多協定支援:SOAP 1.1/1.2、REST/HTTP、JSON、XML 等
  • 高效能:基於 StAX 流式處理,記憶體佔用低

官方網站:Apache CXF 專案首頁

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