• Java调用WebService方法总结(4)--Axis调用WebService


    Axis是比较常用的WebService框架,该项目在2006实现了最终版,后面就没有更新了。文中demo所使用到的软件版本:Java 1.8.0_191、Axis 1.4。

    1、准备

    参考Java调用WebService方法总结(1)--准备工作

    2、调用

    2.1、call方式

    /**
     * call直接调用
     * @param param
     */
    public static void call(String param) {
        try {
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(new URL(url));
            call.setEncodingStyle("utf-8");
            //设置SOAPAction
            call.setUseSOAPAction(true);
            call.setSOAPActionURI("http://webxml.com.cn/toTraditionalChinese");
            //设置Soap协议版本
            call.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
            //call.setSOAPVersion(SOAPConstants.SOAP12_CONSTANTS);
            
            call.addParameter(new QName(targetNamespace, "sText"), XMLType.XSD_STRING, ParameterMode.IN);
            call.setOperationName(new QName(targetNamespace, "toTraditionalChinese"));
            String result = (String) call.invoke(new Object[]{param});
            System.out.println("call结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    2.2、WSDL2Java方式

    2.2.1、org.apache.axis.wsdl.WSDL2Java生成代码

    cmd中执行命令:

    cd %AXIS_HOME%/lib
    java -cp axis.jar;commons-logging-1.0.4.jar;commons-discovery-0.2.jar;jaxrpc.jar;wsdl4j-1.5.1.jar;saaj.jar org.apache.axis.wsdl.WSDL2Java http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl

    生成的代码就在当前目录,包名为cn.com.webxml,文件如下:

    2.2.2、用生成的代码调用WebService

    /**
     * 利用wsdl2java生成代码调用WebService
     * @param param
     */
    public static void wsdl2java(String param) {
        try {
            TraditionalSimplifiedWebService service = new TraditionalSimplifiedWebServiceLocator();
            String result = service.getTraditionalSimplifiedWebServiceSoap().toTraditionalChinese(param);
            System.out.println("wsdl2java soap1.1结果:" + result);
            result = service.getTraditionalSimplifiedWebServiceSoap12().toTraditionalChinese(param);
            System.out.println("wsdl2java soap1.2结果:" + result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    2.2.3、完整代码

    package com.inspur.ws;
    
    import java.net.URL;
    
    import javax.xml.namespace.QName;
    import javax.xml.rpc.ParameterMode;
    
    import org.apache.axis.client.Call;
    import org.apache.axis.client.Service;
    import org.apache.axis.encoding.XMLType;
    import org.apache.axis.soap.SOAPConstants;
    
    
    import cn.com.webxml.TraditionalSimplifiedWebService;
    import cn.com.webxml.TraditionalSimplifiedWebServiceLocator;
    
    /**
     * 
     * Axis调用WebService
     *
     */
    public class Axis {
        private static String url = "http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl";
        private static String targetNamespace = "http://webxml.com.cn/";
        
        /**
         * call直接调用
         * @param param
         */
        public static void call(String param) {
            try {
                Service service = new Service();
                Call call = (Call) service.createCall();
                call.setTargetEndpointAddress(new URL(url));
                call.setEncodingStyle("utf-8");
                //设置SOAPAction
                call.setUseSOAPAction(true);
                call.setSOAPActionURI("http://webxml.com.cn/toTraditionalChinese");
                //设置Soap协议版本
                call.setSOAPVersion(SOAPConstants.SOAP11_CONSTANTS);
                //call.setSOAPVersion(SOAPConstants.SOAP12_CONSTANTS);
                
                call.addParameter(new QName(targetNamespace, "sText"), XMLType.XSD_STRING, ParameterMode.IN);
                call.setOperationName(new QName(targetNamespace, "toTraditionalChinese"));
                String result = (String) call.invoke(new Object[]{param});
                System.out.println("call结果:" + result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        /**
         * 利用wsdl2java生成代码调用WebService
         * @param param
         */
        public static void wsdl2java(String param) {
            try {
                TraditionalSimplifiedWebService service = new TraditionalSimplifiedWebServiceLocator();
                String result = service.getTraditionalSimplifiedWebServiceSoap().toTraditionalChinese(param);
                System.out.println("wsdl2java soap1.1结果:" + result);
                result = service.getTraditionalSimplifiedWebServiceSoap12().toTraditionalChinese(param);
                System.out.println("wsdl2java soap1.2结果:" + result);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public static void main(String[] args) {
            call("小学");
            wsdl2java("大学");
        }
    }
    View Code
  • 相关阅读:
    Spring整合hibernate4:事务管理
    eclipse添加约束文件
    转]@SuppressWarnings 详解
    spring + springMVC +hibernate 配置2
    spring +spring+ hibernate配置1
    eclipse下 hibernate逆向数据库操作示例!!
    Spring+hibernate 配置实例
    dubbo接口快速测试技巧
    Spring Boot Starter列表
    Spring Boot Starters是什么?
  • 原文地址:https://www.cnblogs.com/wuyongyin/p/11851365.html
Copyright © 2020-2023  润新知