VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Java教程 >
  • 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("大学");
    }
}
复制代码

 

来源:https://www.cnblogs.com/wuyongyin/p/11851365.html

 


相关教程