---
title: " Apache CXF简单WebService例子"
author: "cuiran"
date: "2010-12-06T22:09:14Z"
tags: []
categories: ["Web前端"]
slug: "apache-cxf-webservice"
draft: false
lastmod: "2010-12-06T22:11:03Z"
id: 835095
url: "https://cuiran.iteye.com/blog/835095"
---

源码下载地址： [http://dl.dbank.com/c0vhvbrxlr](http://dl.dbank.com/c0vhvbrxlr)

 

本文来自我的CSDN博客，转载请标明出处：[http://blog.csdn.net/cuiran/archive/2010/12/02/6049645.aspx](http://blog.csdn.net/cuiran/archive/2010/12/02/6049645.aspx)

 

 

一.开发环境

我的开发环境是：

MyEclipse 6.5

Tomcat 6.0

Apache cxf-2.3.0 下载地址： [http://www.apache.org/dist/cxf/2.3.0/](http://www.apache.org/dist/cxf/2.3.0/)

二.开发步骤

 

  创建Server

  1. 将下载的cxf-2.3.0 解压。然后在MyEclipse里面添加一个

     User Library名字叫library-cxf-2.3.0  把 lib下的包都添加进去。

  2.新建一个Web工程cxfdemo，并添加library-cxf-2.3.0,然后建个com.demo包。在包里面创建HelloWorld接口

代码如下

view plaincopy to clipboardprint?  
/\**   
 * HelloWorld.java   
 * 版权所有(C) 2010 [cuiran2001@163.com](mailto:cuiran2001@163.com)   
 * 创建:崔冉  Dec 2, 2010 9:47:44 AM   
 \*/   
package com.demo;    
   
import javax.jws.WebService;    
   
/\**   
 * @author 崔冉   
 * @version 1.0.0   
 * @desc 接口HelloWorld   
 \*/   
@WebService   
public interface HelloWorld {    
    String sayHi(String text);    
}   
/\**  
 * HelloWorld.java  
 * 版权所有(C) 2010 [cuiran2001@163.com](mailto:cuiran2001@163.com)  
 * 创建:崔冉  Dec 2, 2010 9:47:44 AM  
 \*/  
package com.demo;

import javax.jws.WebService;

/\**  
 * @author 崔冉  
 * @version 1.0.0  
 * @desc 接口HelloWorld  
 \*/  
@WebService  
public interface HelloWorld {  
 String sayHi(String text);  
}  
 

再创建一个类实现HelloWorld接口

view plaincopy to clipboardprint?  
/\**   
 * HelloWorldImpl.java   
 * 版权所有(C) 2010 [cuiran2001@163.com](mailto:cuiran2001@163.com)   
 * 创建:崔冉  Dec 2, 2010 9:49:17 AM   
 \*/   
package com.demo;    
   
import javax.jws.WebService;    
   
/\**   
 * @author 崔冉   
 * @version 1.0.0   
 * @desc 实现HelloWorld接口   
 \*/   
@WebService   
public class HelloWorldImpl implements HelloWorld {    
   
    /\**   
     *    
     \*/   
    public String sayHi(String text) {    
            
        return "Hi,"+text;    
    }    
   
}   
/\**  
 * HelloWorldImpl.java  
 * 版权所有(C) 2010 [cuiran2001@163.com](mailto:cuiran2001@163.com)  
 * 创建:崔冉  Dec 2, 2010 9:49:17 AM  
 \*/  
package com.demo;

import javax.jws.WebService;

/\**  
 * @author 崔冉  
 * @version 1.0.0  
 * @desc 实现HelloWorld接口  
 \*/  
@WebService  
public class HelloWorldImpl implements HelloWorld {

 /\**  
  *  
  \*/  
 public String sayHi(String text) {  
    
  return "Hi,"+text;  
 }

}  
 

然后修改web.xml里面内容

view plaincopy to clipboardprint?  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;   
&lt;web-app&gt;   
  &lt;context-param&gt;   
        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;   
        &lt;param-value&gt;WEB-INF/beans.xml&lt;/param-value&gt;   
    &lt;/context-param&gt;   
   
    &lt;listener&gt;   
        &lt;listener-class&gt;   
            org.springframework.web.context.ContextLoaderListener    
        &lt;/listener-class&gt;   
    &lt;/listener&gt;   
   
    &lt;servlet&gt;   
        &lt;servlet-name&gt;CXFServlet&lt;/servlet-name&gt;   
        &lt;display-name&gt;CXF Servlet&lt;/display-name&gt;   
        &lt;servlet-class&gt;   
            org.apache.cxf.transport.servlet.CXFServlet    
        &lt;/servlet-class&gt;   
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;   
    &lt;/servlet&gt;   
   
    &lt;servlet-mapping&gt;   
        &lt;servlet-name&gt;CXFServlet&lt;/servlet-name&gt;   
        &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;   
    &lt;/servlet-mapping&gt;   
   
&lt;/web-app&gt;   
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;web-app&gt;  
  &lt;context-param&gt;  
        &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;  
        &lt;param-value&gt;WEB-INF/beans.xml&lt;/param-value&gt;  
    &lt;/context-param&gt;

    &lt;listener&gt;  
        &lt;listener-class&gt;  
            org.springframework.web.context.ContextLoaderListener  
        &lt;/listener-class&gt;  
    &lt;/listener&gt;

    &lt;servlet&gt;  
        &lt;servlet-name&gt;CXFServlet&lt;/servlet-name&gt;  
        &lt;display-name&gt;CXF Servlet&lt;/display-name&gt;  
        &lt;servlet-class&gt;  
            org.apache.cxf.transport.servlet.CXFServlet  
        &lt;/servlet-class&gt;  
        &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;  
    &lt;/servlet&gt;

    &lt;servlet-mapping&gt;  
        &lt;servlet-name&gt;CXFServlet&lt;/servlet-name&gt;  
        &lt;url-pattern&gt;/\*&lt;/url-pattern&gt;  
    &lt;/servlet-mapping&gt;

&lt;/web-app&gt;  
 

在WEB-INF下面创建一个beans.xml

代码如下

view plaincopy to clipboardprint?  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;   
&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"   
    xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"   
    xmlns:jaxws="[http://cxf.apache.org/jaxws](http://cxf.apache.org/jaxws)"   
    xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)     
    [http://www.springframework.org/schema/beans/spring-beans.xsd](http://www.springframework.org/schema/beans/spring-beans.xsd)    
    [http://cxf.apache.org/jaxws](http://cxf.apache.org/jaxws) [http://cxf.apache.org/schemas/jaxws.xsd](http://cxf.apache.org/schemas/jaxws.xsd)"&gt;   
   
    &lt;import resource="classpath:META-INF/cxf/cxf.xml" /&gt;   
    &lt;import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /&gt;   
    &lt;import resource="classpath:META-INF/cxf/cxf-servlet.xml" /&gt;   
   
    &lt;jaxws:endpoint id="helloWorld" implementor="com.demo.HelloWorldImpl" address="/HelloWorld" /&gt;   
&lt;/beans&gt;   
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
    xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
    xmlns:jaxws="[http://cxf.apache.org/jaxws](http://cxf.apache.org/jaxws)"  
    xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)  
    [http://www.springframework.org/schema/beans/spring-beans.xsd](http://www.springframework.org/schema/beans/spring-beans.xsd)  
    [http://cxf.apache.org/jaxws](http://cxf.apache.org/jaxws) [http://cxf.apache.org/schemas/jaxws.xsd](http://cxf.apache.org/schemas/jaxws.xsd)"&gt;

    &lt;import resource="classpath:META-INF/cxf/cxf.xml" /&gt;  
    &lt;import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /&gt;  
    &lt;import resource="classpath:META-INF/cxf/cxf-servlet.xml" /&gt;

    &lt;jaxws:endpoint id="helloWorld" implementor="com.demo.HelloWorldImpl" address="/HelloWorld" /&gt;  
&lt;/beans&gt;

 

将Web工程发布到Tomcat里面，然后启动Server

 

  创建Client

  接着创建客户端，在com.demo下面创建client-beans.xml。内容如下

view plaincopy to clipboardprint?  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;   
&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"   
    xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"   
    xmlns:jaxws="[http://cxf.apache.org/jaxws](http://cxf.apache.org/jaxws)"   
    xsi:schemaLocation="    
[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans.xsd](http://www.springframework.org/schema/beans/spring-beans.xsd)    
[http://cxf.apache.org/jaxws](http://cxf.apache.org/jaxws) [http://cxf.apache.org/schema/jaxws.xsd](http://cxf.apache.org/schema/jaxws.xsd)"&gt;   
   
    &lt;bean id="client" class="com.demo.HelloWorld"   
        factory-bean="clientFactory" factory-method="create" /&gt;   
   
    &lt;bean id="clientFactory"   
        class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"&gt;   
        &lt;property name="serviceClass" value="com.demo.HelloWorld" /&gt;   
        &lt;property name="address" value="[http://127.0.0.1:8080/cxfdemo/HelloWorld](http://127.0.0.1:8080/cxfdemo/HelloWorld)" /&gt;   
    &lt;/bean&gt;   
   
&lt;/beans&gt;   
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
    xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
    xmlns:jaxws="[http://cxf.apache.org/jaxws](http://cxf.apache.org/jaxws)"  
    xsi:schemaLocation="  
[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans) [http://www.springframework.org/schema/beans/spring-beans.xsd](http://www.springframework.org/schema/beans/spring-beans.xsd)  
[http://cxf.apache.org/jaxws](http://cxf.apache.org/jaxws) [http://cxf.apache.org/schema/jaxws.xsd](http://cxf.apache.org/schema/jaxws.xsd)"&gt;

    &lt;bean id="client" class="com.demo.HelloWorld"  
        factory-bean="clientFactory" factory-method="create" /&gt;

    &lt;bean id="clientFactory"  
        class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"&gt;  
        &lt;property name="serviceClass" value="com.demo.HelloWorld" /&gt;  
        &lt;property name="address" value="[http://127.0.0.1:8080/cxfdemo/HelloWorld](http://127.0.0.1:8080/cxfdemo/HelloWorld)" /&gt;  
    &lt;/bean&gt;

&lt;/beans&gt;

 

在创建测试类Client.java

view plaincopy to clipboardprint?  
/\**   
 * Client.java   
 * 版权所有(C) 2010 [cuiran2001@163.com](mailto:cuiran2001@163.com)   
 * 创建:崔冉  Dec 2, 2010 9:53:30 AM   
 \*/   
package com.demo;    
   
import org.springframework.context.ApplicationContext;    
import org.springframework.context.support.ClassPathXmlApplicationContext;    
   
/\**   
 * @author 崔冉   
 * @version 1.0.0   
 * @desc 测试类   
 \*/   
public class Client {    
      public static Client self = new Client();    
      private HelloWorld client = null;    
          
     private Client() {    
            // START SNIPPET: client    
            ApplicationContext context = new ClassPathXmlApplicationContext(    
                    new String\[]{"com/demo/client-beans.xml"});    
            client = (HelloWorld) context.getBean("client");    
        }    
          
   
        public HelloWorld getServer() {    
                
            return client;    
                
        }    
   
        public String getText(String text) throws Exception {    
            String response = getServer().sayHi(text);    
            return response;    
        }    
   
   
    /\**   
     * @param args   
     \*/   
    public static void main(String\[] args) {    
        // TODO Auto-generated method stub    
        try {    
                
            System.out.println(self.getText("cuiran"));    
        } catch (Exception e) {    
            // TODO Auto-generated catch block    
            e.printStackTrace();    
        }    
            
    }    
   
}   
/\**  
 * Client.java  
 * 版权所有(C) 2010 [cuiran2001@163.com](mailto:cuiran2001@163.com)  
 * 创建:崔冉  Dec 2, 2010 9:53:30 AM  
 \*/  
package com.demo;

import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;

/\**  
 * @author 崔冉  
 * @version 1.0.0  
 * @desc 测试类  
 \*/  
public class Client {  
   public static Client self = new Client();  
   private HelloWorld client = null;  
    
  private Client() {  
         // START SNIPPET: client  
         ApplicationContext context = new ClassPathXmlApplicationContext(  
                 new String\[]{"com/demo/client-beans.xml"});  
         client = (HelloWorld) context.getBean("client");  
     }  
  

     public HelloWorld getServer() {  
        
         return client;  
          
     }

     public String getText(String text) throws Exception {  
         String response = getServer().sayHi(text);  
         return response;  
     }

 /\**  
  * @param args  
  \*/  
 public static void main(String\[] args) {  
  // TODO Auto-generated method stub  
  try {  
     
   System.out.println(self.getText("cuiran"));  
  } catch (Exception e) {  
   // TODO Auto-generated catch block  
   e.printStackTrace();  
  }  
    
 }

}  
 

 

    然后进行测试运行。如果遇到类似这种错误“AXB 2.1 API is being loaded from the bootstrap classloader” 说明你的web工程添加了J2EE 1.4或者是1.5 library. 这样就与jaxb-api-2.2.1.jar

  冲突了，只需删除即可。

最后运行如图

 

 

源码下载地址： [http://dl.dbank.com/c0vhvbrxlr](http://dl.dbank.com/c0vhvbrxlr)

 

本文来自我的CSDN博客，转载请标明出处：[http://blog.csdn.net/cuiran/archive/2010/12/02/6049645.aspx](http://blog.csdn.net/cuiran/archive/2010/12/02/6049645.aspx)