JAVA_HOME、CATALINA_HOME、ANT_HOME、CXF_HOME%JAVA_HOME%\bin;%CATALINA_HOME%\bin;%ANT_HOME%\binApache CXF 是開源 Web 服務框架,支持 JAX-WS、SOAP、WSDL、MTOM、WS-* 全棧標準,可部署於 Tomcat 等輕量容器,也能運行於 JBoss、WebSphere、WebLogic 等應用服務器。核心特性:
package demo.order;
import javax.jws.WebService;
@WebService
public interface OrderProcess {
String processOrder(Order order);
}
package demo.order;
import javax.jws.WebService;
@WebService(endpointInterface = "demo.order.OrderProcess")
public class OrderProcessImpl implements OrderProcess {
public String processOrder(Order order) {
return order.validate(); // 返回訂單 ID
}
}
beans.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">
<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:endpoint
id="orderProcess"
implementor="demo.order.OrderProcessImpl"
address="/OrderProcess"/>
</beans>
web.xml<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
client-beans.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-2.0.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="demo.order.OrderProcess"/>
<property name="address" value="https://localhost:8080/orderapp/OrderProcess"/>
</bean>
<bean id="client" class="demo.order.OrderProcess"
factory-bean="clientFactory" factory-method="create"/>
</beans>
public final class Client {
public static void main(String[] args) throws Exception {
ClassPathXmlApplicationContext ctx =
new ClassPathXmlApplicationContext("demo/order/client/client-beans.xml");
OrderProcess client = (OrderProcess) ctx.getBean("client");
Order order = new Order();
String orderID = client.processOrder(order);
System.out.println("Order ID: " + orderID);
System.exit(0);
}
}

ant deploy 生成 orderapp.war 部署至 Tomcat webapps。catalina start。ant client 運行客戶端,控制台輸出訂單 ID。
CXF 以極少代碼實現 Web 服務發布與調用,Spring 整合讓配置更聲明式。下一部分將介紹如何用 CXF + Spring 將 POJO 發布為 RESTful 服務。