---
title: "使用Apache CXF创建Web Service"
author: "lanmh"
date: "2009-12-30T09:30:52Z"
tags: []
categories: ["企业架构"]
slug: "apache-cxf-web-service"
draft: false
lastmod: "2009-12-30T09:30:52Z"
id: 559629
url: "https://lanmh.iteye.com/blog/559629"
---

官方主页：http://cxf.apache.org/

 下载后内附详细的sample。这里先演示一个最简单的工程。

 如下图所示建立工程：

![使用Apache CXF创建Web Service](http://img.ddvip.com/2009_02_04/1233727826_ddvip_6209..jpg)

　　客户端测试代码：TestServiceClient.java

　　packagejp.co.apm.client;

`importjp.co.apm.service.TestService; 　　 importorg.apache.cxf.frontend.ClientProxyFactoryBean; 　　 publicclassTestServiceClient{ 　　 　　publicstaticvoidmain(String[]args){ 　　　　ClientProxyFactoryBeanfactory=newClientProxyFactoryBean(); 　　　　factory.setServiceClass(TestService.class); 　　　　factory.setAddress("http://localhost:8080/APM_CXF/services/test"); 　　　　TestServiceservice=(TestService)factory.create(); 　　　　System.out.println(service.sayHello()); 　　} }`

　　TestServiceImpl.java

`packagejp.co.apm.service.impl; 　　 importjp.co.apm.service.TestService; 　　 publicclassTestServiceImplimplementsTestService{ 　　 　　publicStringsayHello(){ 　　　　return"Hello,ShenBin"; 　　} }`

　　TestService.java

`packagejp.co.apm.service; 　　 publicinterfaceTestService{ 　　 　　publicStringsayHello(); }`

　　cxf-servlet.xml

`<?xmlversion="1.0"encoding="UTF-8"?> <beansxmlns="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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://cxf.apache.org/bindings/soaphttp://cxf.apache.org/schemas/configuration/soap.xsd http://cxf.apache.org/simplehttp://cxf.apache.org/schemas/simple.xsd"> 　　 　　<simple:serverid="testservice"serviceClass="jp.co.apm.service.TestService"address="/test"> 　　　　<simple:serviceBean> 　　　　　　<beanclass="jp.co.apm.service.impl.TestServiceImpl"/> 　　　　</simple:serviceBean> 　　</simple:server> 　　 </beans>`

　　web.xml

`<?xmlversion="1.0"encoding="UTF-8"?> <web-appversion="2.5"xmlns="http://java.sun.com/xml/ns/javaee" 　　xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 　　xsi:schemaLocation="http://java.sun.com/xml/ns/javaee　 　　http://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>`

 启动Tomcat，访问： http://localhost:8080/APM\_CXF/services/test?wsdl

　　运行TestServiceClient.java测试结果。