---
title: "CXF  WSDL2JAVA"
author: "solomon"
date: "2010-12-24T10:57:19Z"
tags: []
categories: ["非技术"]
slug: "cxf-wsdl2java"
draft: false
lastmod: "2010-12-24T11:31:35Z"
id: 850937
url: "https://solomon.iteye.com/blog/850937"
---

转自 : [http://blog.chinaunix.net/u2/73798/showart_2002108.html](http://blog.chinaunix.net/u2/73798/showart_2002108.html)




将wdl  binder  [**http://service.test.com/**](http://service.test.com/)替换为自己的namespace。


命令格式如下：

wsdl2java -b jaxb-binding-date.xml http：//localhost:8080/cxfservice/services/XXXXX?wsdl

#########################################################

**CXF wsdl2Java**

![](/admin/templates/default/images/right_line.gif&quot; border=&quot;0&quot; alt=&quot;&quot; width=&quot;502&quot; height=&quot;9)



一.  简介
Apache CXF 是一个Service框架，他简化了Service的创建， CXF实现了JAX-WS2.0规范，并通过了JAX-WS2.0 TCK； CXF和Spring无缝集成；CXF支持多种传输协议（HTTP， JMS， Corba等）， 支持多种Binding数据格式（SOAP，XML，JSON等）， 支持多种DataBinding数据类型（JAXB， Aegis) 。CXF基于Interceptor的架构，使得整个框架非常易于扩展。


二.  如何发布并调用简单的web service实例

2.1．下载：apache-cxf-2.1.1  [http://cxf.apache.org/download.html](http://cxf.apache.org/download.html) 

2.2． 新建java project ,并加入apache-cxf-2.0.7\lib所有包，编写要发布的web service 接口和实现
import javax.jws.WebService;
@WebService  
public interface HelloWorld {   
     public String sayHello(String text);   
}
import javax.jws.WebService;   
@WebService(endpointInterface=&quot;test.HelloWorld&quot;)   
public class HelloWorldImpl implements HelloWorld {   
      public String sayHello(String text) {   
                  return &quot;Hello&quot; + text ;   
    }   
  }  
@WebService 注解表示是要发布的web 服务
name：用于Interface，属映射到wsdl:portType element的name属性。 
targetNamespace：用于Interface和implement，如果不指定，缺省会使用包名倒序做为wsdl名空间。
serviceName：用于implement，表示wsdl服务名。 
portName：用于implement，表示wsdl:port 的name属性。
endpointInterface：用于implement，指定Interface全名，包括包名。


2.3.发布web service
public class Server {
    protected Server() throws Exception {
         System.out.println(&quot;Starting Server&quot;);
         HelloWorldImpl implementor = new HelloWorldImpl();
         String address = &quot;[http://localhost:9000/helloWorld](http://localhost:9000/helloWorld)&quot;;
         Endpoint.publish(address, implementor);
    }
    public static void main(String args) throws Exception {
        new Server();
        System.out.println(&quot;Server ready...&quot;);
        Thread.sleep(5 * 60 * 1000);
        System.out.println(&quot;Server exiting&quot;);
        System.exit(0);
    }
}
运行后，在浏览器中输入[http://localhost:9000/helloWorld?wsdl](http://localhost:9000/helloWorld?wsdl)将显示这个web service的wsdl.说明web service发布成功。


2.4.下面就开始创建一个客户端程序，访问这个web service, 同样新建java project ,并加入apache-cxf-2.0.7\lib所有包，由于CXF已经提供wsdl转化成java 的命令工具，所以创建一个build.xml，用来生成客户端程序。Bulid.xml内容如下：

<?xml version="1.0"?>
<project name="cxf wsdl2java" basedir=".">   
   <property name="cxf.home" location ="${basedir}/WebRoot/WEB-INF/"/>
   <path id="cxf.classpath">
      <fileset dir="${cxf.home}/lib">
         <include name="*.jar"/>
      </fileset>
   </path>      
   <target name="cxfWSDLToJava">
      <java classname="org.apache.cxf.tools.wsdlto.WSDLToJava" fork="true">
         <arg value="-client"/>
         <arg value="-d"/>
         <arg value="src"/>
         <arg value="http://localhost:9000/helloWorld?wsdl"/>
         <classpath>
            <path refid="cxf.classpath"/>
         </classpath>
      </java>
   </target>
</project>

或者：<span style="color:red">**配置环境变量%CXF_HOME%=E:\WebService\CXF\apache-cxf-2.1.1\apache-cxf-2.1.1（以我的目录为例）,并在PATH后加上;%CXF_HOME%\bin**</span>在cmd命令行中输入wsdl2java如果显示其用法表示配置好了。
输入：<span style="color:red">**wsdl2java -d src - client [http://localhost:9000/helloWorld?wsdl](http://localhost:9000/helloWorld?wsdl) **</span>其作用上面的build.xml作用一样。
附加：wsdl2java用法：
wsdl2java -p com -d src -all  aa.wsdl 
-p  指定其wsdl的命名空间，也就是要生成代码的包名:
-d  指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web  service的代码
-impl 生成web service的实现代码
-ant  生成build.xml文件
-all 生成所有开始端点代码：types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file. 
详细用法见：[http://cwiki.apache.org/CXF20DOC/wsdl-to-java.html](http://cwiki.apache.org/CXF20DOC/wsdl-to-java.html) 
2.5.调用web service
public class MyClient {
      public static void main(String argv) {
        HelloWorld  hello = new HelloWorldImplService().getHelloWorldImplPort();
        System.out.println(hello.sayHello(&quot;Tom&quot;) );  
      }
    }
注意：运行时，要一定先要发布web sevice.
三.  参考资料
1.CXF 主页: [http://cxf.apache.org/](http://cxf.apache.org/) 
2. CXF中文讨论组: [http://groups.google.com/group/cxf-zh](http://groups.google.com/group/cxf-zh)  
3. Web service: [http://www.w3school.com.cn/webservices/index.asp](http://www.w3school.com.cn/webservices/index.asp)
4. WSDL: [http://www.w3school.com.cn/wsdl/index.asp](http://www.w3school.com.cn/wsdl/index.asp)
5. SOAP:http://www.w3school.com.cn/soap/index.asp