使用CXF自动生成webservice客户端代码

首先当前是从官网下载cxf组件.  

  1. http://cxf.apache.org/download.html  

下载后解压,在这里主要是用到解压后的bin目录中的wsdl2java.bat该批处理文件.

 

可以直接进入bin目下,运行wsdl2java,需要注意的他的几个参数

我测试时直接运行的以下命令:

 

wsdl2java -p com.zzzl.webservice.qidian -d d:\cxfoutput\src -all  http://game.qidian.com/RemoteWebService/IPreventIndulge.asmx?wsdl

参数说明:

-p 也就是package 对应java中的包

-d 输入目录,生成.java文件会在该目录,会自动添加-p参数配置的包路径

-client 生成客户端测试web service的代码.

-server 生成服务器启动web  service的代码.

-impl 生成web service的实现代码.

-ant  生成build.xml文件.

-all 生成上面-client -server -impl -ant 对应的所有文件.

注:

1.如果在调用.NET WebService的时候报( undefinedelement declaration 's:schema'),可参考如下网址:

 http://blog.csdn.net/binbinxyz/article/details/8906717

即是wsdl2java是通过JAXB解析wsdl文件的,JAXB目前还不支持ref 这种元素的解析,故可以根据

http://game.qidian.com/RemoteWebService/IPreventIndulge.asmx?wsdl 此地址另存为.wsdl文件,然后用<s:any minOccurs="2" maxOccurs="2"/>替换wsdl文件中<s:element ref="s:schema" /><s:any />元素。

2.通过上面步骤后即可通过wsdl2java命令,生成对应的java代码,然后把另存为的.wsdl文件拷贝到classes目录下对应的java目录下(和生成的Java代码同级)

3.当前测试使用apache-cxf-2.6.16成功生成ws客户端代码,cxf可以到百度网盘中下载

4.新版本生成的cxf组件生成的代码URL处测试时候会报文件找不到,修改参考如下:

   

static {
        URL url = null;
        try {
            url = new URL("file:SubStation.wsdl");
            //url = new URL("file:d:\SubStation.wsdl"); 正确写法
        } catch (MalformedURLException e) {
            java.util.logging.Logger.getLogger(SubStation.class.getName())
                .log(java.util.logging.Level.INFO, 
                     "Can not initialize the default wsdl from {0}", "file:SubStation.wsdl");
        }
        WSDL_LOCATION = url;
    }

    改成:

static {
        URL url = SubStation.class.getResource("SubStation.wsdl");
        if (url == null) {
            url = SubStation.class.getClassLoader().getResource("SubStation.wsdl");
        } 
        if (url == null) {
            java.util.logging.Logger.getLogger(SubStation.class.getName())
                .log(java.util.logging.Level.INFO, 
                     "Can not initialize the default wsdl from {0}", "SubStation.wsdl");
        }       
        WSDL_LOCATION = url;
    }

猜你喜欢

转载自gqsunrise.iteye.com/blog/2220625