Mac下使用 Apache CXF自动生成WebService客户端java代码并调用介绍

Apache CXF自动生成WebService客户端java代码并调用介绍

1. CXF环境的配置

1、下载CXF的zip包,解压。
3、配置环境变量 
$ vi .zshrc
export CXF_HOME=/Users/qiuzhiqing/2-my-tools/apache-cxf-3.3.6
export CXF3=$CXF_HOME/bin
export PATH=$CXF3:$PATH

$ source .zshrc


2. Wsdl文件的准备(2种方式)

以中国股票行情分时走势预览缩略图为例:
1、可以直接利用url,不需要文件,
http://www.webxml.com.cn/webservices/ChinaStockSmallImageWS.asmx?wsdl

2、文件:访问wsdl接口,下载页面内容。然后根据需要的接口数据定义,
   去掉一些无用的接口定义。不然生成无用的对象。
   得到 ChinaStockSmallImageWS.asmx?wsdl
   

3.Mac修改JDK路径

a. 查看jdk安装路径,举个栗子
 /Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home


b. 找到并修改 bin/wsdl2java 中的变量JAVA_HOME和CXF_HOME为本地环境路径
...
#!/bin/sh
#
#
#    Licensed to the Apache Software Foundation (ASF) under one
#    or more contributor license agreements. See the NOTICE file
#    distributed with this work for additional information
#    regarding copyright ownership. The ASF licenses this file
#    to you under the Apache License, Version 2.0 (the
#    "License"); you may not use this file except in compliance
#    with the License. You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing,
#    software distributed under the License is distributed on an
#    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#    KIND, either express or implied. See the License for the
#    specific language governing permissions and limitations
#    under the License.
#
#
# run the Apache CXF wsdl2java tool 
#


# Check for irritating 'operating systems'.
cygwin=false;
darwin=false;
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home

#case "`uname`" in
#  CYGWIN*) cygwin=true ;;
#  Darwin*) darwin=true
#           if [ -z "$JAVA_HOME" ] ; then
#             JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home
#           fi
#           ;;
#esac
#
#
# Load common functions
#
DIR="$( cd "$( dirname $0 )" >/dev/null 2>&1 && pwd )"
. "${DIR}/inc"

# Determine the JVM version >= 9
checkJvmVersion

me=`basename $0`
CXF_HOME=/Users/qiuzhiqing/2-my-tools/apache-cxf-3.3.6
cxf_home=$CXF_HOME
if [ ! -f  $cxf_home/lib/cxf-manifest.jar ]; then
    cxf_home=`dirname $0`/..
fi

cxf_jar=$cxf_home/lib/cxf-manifest.jar

if [ ! -f $cxf_jar ]; then 
    if [ ! -f ${cxf_home}/../../../target/srcbuild_env ]; then
        echo "ERROR: Unable to find cxf-manifest.jar in $cxf_home/lib"
        exit 1
    else
        . ${cxf_home}/../../../target/srcbuild_env
        cxf_jar=${CXF_CLASSPATH}
    fi
fi 

#
# add the jdk's tools.jar to the classpath
#

log_config=$cxf_home/etc/logging.properties 
if $cygwin; then
  if [ "$OS" = "Windows_NT" ] && cygpath -m .>/dev/null 2>/dev/null ; then
    format=mixed
  else
    format=windows
  fi
  cxf_classpath=`cygpath --$format "${cxf_jar}"`
  if [ ! -z "${CLASSPATH}" ]; then
    cxf_classpath=${cxf_classpath};`cygpath --$format --path "${CLASSPATH}"`
  fi
  if [ ! -z "${sun_tool_path}" ] ; then
    cxf_classpath=${cxf_classpath};`cygpath --$format "${sun_tool_path}"`
  fi
  log_config=`cygpath --$format "$log_config"`
else
   cxf_classpath=${cxf_jar}:${CLASSPATH}:${sun_tool_path}
fi

if [ "x${JAVA_MAX_MEM}" = "x" ]; then
    JAVA_MAX_MEM=512M
    export JAVA_MAX_MEM
fi

if [ "${VERSION}" -gt "8" ]; then
    $JAVA_HOME/bin/java -Xmx${JAVA_MAX_MEM} -cp "${cxf_classpath}" -Djava.util.logging.config.file=$log_config org.apache.cxf.tools.wsdlto.WSDLToJava "$@"
else
    $JAVA_HOME/bin/java -Xmx${JAVA_MAX_MEM} -Djava.endorsed.dirs="${cxf_home}/lib/endorsed" -cp "${cxf_classpath}" -Djava.util.logging.config.file=$log_config org.apache.cxf.tools.wsdlto.WSDLToJava "$@"
fi

4. 代码生成并调用

a. 切换任意目录生成代码目录
cd /x/y/z

b. 方式1:uri生成
wsdl2java -d ./src -client  'http://www.webxml.com.cn/webservices/ChinaStockSmallImageWS.asmx?wsdl'

c. 方式2:file生成
wsdl2java -d ./src -client  ./ChinaStockSmallImageWS.asmx?wsdl

d. 生成代码目录
-rw-r--r--  1 qiuzhiqing  staff  1947  4 24 10:47 GetSmallImage.java
-rw-r--r--  1 qiuzhiqing  staff  1955  4 24 10:47 GetSmallImageByte.java
-rw-r--r--  1 qiuzhiqing  staff  1496  4 24 10:47 GetSmallImageByteResponse.java
-rw-r--r--  1 qiuzhiqing  staff   725  4 24 10:47 GetSmallImageResponse.java
-rw-r--r--  1 qiuzhiqing  staff  2390  4 24 10:47 ObjectFactory.java
-rw-r--r--  1 qiuzhiqing  staff   165  4 24 10:47 package-info.java
-rw-r--r--  1 qiuzhiqing  staff  2362  4 24 10:47 ChinaStockSmallImageWSSoap_ChinaStockSmallImageWSSoap12_Client.java
-rw-r--r--  1 qiuzhiqing  staff  2356  4 24 10:47 ChinaStockSmallImageWSSoap_ChinaStockSmallImageWSSoap_Client.java
-rw-r--r--  1 qiuzhiqing  staff  2320  4 24 10:47 ChinaStockSmallImageWSHttpGet_ChinaStockSmallImageWSHttpGet_Client.java
-rw-r--r--  1 qiuzhiqing  staff  2326  4 24 10:47 ChinaStockSmallImageWSHttpPost_ChinaStockSmallImageWSHttpPost_Client.java
-rw-r--r--  1 qiuzhiqing  staff  2545  4 24 10:47 ChinaStockSmallImageWSSoap.java
-rw-r--r--  1 qiuzhiqing  staff  2026  4 24 10:47 ChinaStockSmallImageWSHttpGet.java
-rw-r--r--  1 qiuzhiqing  staff  2028  4 24 10:47 ChinaStockSmallImageWSHttpPost.java
-rw-r--r--  1 qiuzhiqing  staff  7438  4 24 10:47 ChinaStockSmallImageWS.java



5. 调用客户端可以选择xxx_Client.java中代码main函数作为参考

% cat ChinaStockSmallImageWSSoap_ChinaStockSmallImageWSSoap_Client.java

package cn.com.webxml;

/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 3.3.6
 * 2020-04-24T10:47:24.499+08:00
 * Generated source version: 3.3.6
 *
 */
public final class ChinaStockSmallImageWSSoap_ChinaStockSmallImageWSSoap_Client {

    private static final QName SERVICE_NAME = new QName("http://WebXml.com.cn/", "ChinaStockSmallImageWS");

    private ChinaStockSmallImageWSSoap_ChinaStockSmallImageWSSoap_Client() {
    }

    public static void main(String args[]) throws java.lang.Exception {
        URL wsdlURL = ChinaStockSmallImageWS.WSDL_LOCATION;
        if (args.length > 0 && args[0] != null && !"".equals(args[0])) {
            File wsdlFile = new File(args[0]);
            try {
                if (wsdlFile.exists()) {
                    wsdlURL = wsdlFile.toURI().toURL();
                } else {
                    wsdlURL = new URL(args[0]);
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }

        ChinaStockSmallImageWS ss = new ChinaStockSmallImageWS(wsdlURL, SERVICE_NAME);
        ChinaStockSmallImageWSSoap port = ss.getChinaStockSmallImageWSSoap();

        {
        System.out.println("Invoking getSmallImage...");
        java.lang.String _getSmallImage_theStockCode = "";
        short _getSmallImage_theImageType = Short.parseShort("0");
        port.getSmallImage(_getSmallImage_theStockCode, _getSmallImage_theImageType);


        }
        {
        System.out.println("Invoking getSmallImageByte...");
        java.lang.String _getSmallImageByte_theStockCode = "";
        short _getSmallImageByte_theImageType = Short.parseShort("0");
        byte[] _getSmallImageByte__return = port.getSmallImageByte(_getSmallImageByte_theStockCode, _getSmallImageByte_theImageType);
        System.out.println("getSmallImageByte.result=" + _getSmallImageByte__return);


        }

        System.exit(0);
    }

}

原创文章,如果你觉得写的还可以帮助到您,可以请作者喝杯咖啡,谢谢!

原文地址:https://www.cnblogs.com/golden-elephant/p/12766085.html