当前位置: 首页 > 工具软件 > Apache CXF > 使用案例 >

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

宋伯寅
2023-12-01

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= C X F H O M E / b i n e x p o r t P A T H = CXF_HOME/bin export PATH= CXFHOME/binexportPATH=CXF3:$PATH

$ source .zshrc

  1. 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=" ( c d " ( cd " (cd"( dirname KaTeX parse error: Expected 'EOF', got '&' at position 19: …" >/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
. c x f h o m e / . . / . . / . . / t a r g e t / s r c b u i l d e n v c x f j a r = {cxf_home}/../../../target/srcbuild_env cxf_jar= cxfhome/../../../target/srcbuildenvcxfjar={CXF_CLASSPATH}
fi
fi

add the jdk’s tools.jar to the classpath

log_config=$cxf_home/etc/logging.properties
if c y g w i n ; t h e n i f [ " cygwin; then if [ " cygwin;thenif["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 " C L A S S P A T H " ] ; t h e n c x f c l a s s p a t h = {CLASSPATH}" ]; then cxf_classpath= CLASSPATH"];thencxfclasspath={cxf_classpath};cygpath --$format --path "${CLASSPATH}"
fi
if [ ! -z " s u n t o o l p a t h " ] ; t h e n c x f c l a s s p a t h = {sun_tool_path}" ] ; then cxf_classpath= suntoolpath"];thencxfclasspath={cxf_classpath};cygpath --$format "${sun_tool_path}"
fi
log_config=cygpath --$format "$log_config"
else
cxf_classpath= c x f j a r : {cxf_jar}: cxfjar:{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
J A V A H O M E / b i n / j a v a − X m x JAVA_HOME/bin/java -Xmx JAVAHOME/bin/javaXmx{JAVA_MAX_MEM} -cp “ c x f c l a s s p a t h " − D j a v a . u t i l . l o g g i n g . c o n f i g . f i l e = {cxf_classpath}" -Djava.util.logging.config.file= cxfclasspath"Djava.util.logging.config.file=log_config org.apache.cxf.tools.wsdlto.WSDLToJava “$@”
else
J A V A H O M E / b i n / j a v a − X m x JAVA_HOME/bin/java -Xmx JAVAHOME/bin/javaXmx{JAVA_MAX_MEM} -Djava.endorsed.dirs=” c x f h o m e / l i b / e n d o r s e d " − c p " {cxf_home}/lib/endorsed" -cp " cxfhome/lib/endorsed"cp"{cxf_classpath}" -Djava.util.logging.config.file= l o g c o n f i g o r g . a p a c h e . c x f . t o o l s . w s d l t o . W S D L T o J a v a " log_config org.apache.cxf.tools.wsdlto.WSDLToJava " logconfigorg.apache.cxf.tools.wsdlto.WSDLToJava"@"
fi

  1. 代码生成并调用
    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

  1. 调用客户端可以选择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);
}

}

 类似资料: