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

Spring 整合 Apache CXF 發布與消費 WebService 完整實戰

     編輯:Java 相關知識

服務端構建

依賴準備

  • CXF 核心包:cxf-rt-frontend-jaxws、cxf-rt-transports-http
  • Spring 上下文:spring-web、spring-context

web.xml 配置 CXF Servlet

<servlet>
  <servlet-name>cxf</servlet-name>
  <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  <init-param>
    <param-name>config-location</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>cxf</servlet-name>
  <url-pattern>/webservice/*</url-pattern>
</servlet-mapping>

Spring 配置 applicationContext.xml

<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">

  <!-- 服務實現 Bean -->
  <bean id="helloService" class="com.example.HelloServiceImpl"/>

  <!-- 發布端點 -->
  <jaxws:endpoint id="helloEndpoint"
                  implementor="#helloService"
                  address="/hello" />
</beans>

服務接口與實現

@WebService
public interface HelloService {
    String sayHello(String name, int age);
}

@WebService(endpointInterface = "com.example.HelloService")
public class HelloServiceImpl implements HelloService {
    public String sayHello(String name, int age) {
        return "Hello " + name + ", age: " + age;
    }
}

驗證發布

啟動項目後訪問:https://localhost:8080/webservice/hello?wsdl 獲取 WSDL 文檔

客戶端調用兩種方式

方式一:wsdl2java 生成代碼調用

# 命令行生成客戶端存根
wsdl2java -d src/main/java -client https://localhost:8080/webservice/hello?wsdl

生成的代碼包含:服務接口、Service 工廠類、異常類等。直接 new Service 獲取 Port 調用。

方式二:Spring 配置代理(推薦)

<beans xmlns:jaxws="https://cxf.apache.org/jaxws" ...>
  <jaxws:client id="helloClient"
                serviceClass="com.example.HelloService"
                address="https://localhost:8080/webservice/hello" />
</beans>
public class ClientTest {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = 
            new ClassPathXmlApplicationContext("client-context.xml");
        HelloService proxy = ctx.getBean("helloClient", HelloService.class);
        System.out.println(proxy.sayHello("測試", 25));
    }
}

進階配置技巧

  • 超時控制:<jaxws:client> 子元素 <jaxws:properties> 設置 connectionTimeout/receiveTimeout
  • WS-Security:添加 cxf-rt-ws-security,配置 WSS4JInInterceptor/OutInterceptor
  • 日誌攔截:<cxf:bus> 掛載 LoggingInInterceptor/LoggingOutInterceptor
  • MTOM 大附件:服務端 @MTOM 註解,客戶端啟用 mtom-enabled="true"

常見問題排查

現象原因解決
404 Not Foundurl-pattern / address 不匹配檢查 /webservice/* + address="/hello" → 完整路徑 /webservice/hello
ClassCastException客戶端/服務端模型類版本不一致共享模型 JAR 或強制同步 serialVersionUID
命名空間不匹配WSDL targetNamespace 與代碼不符@WebService(targetNamespace="...") 顯式指定
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved