java调用webservice接口的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/KindergartenBoss/article/details/81905208

一.利用eclipse工具测试

1.用eclipse新建一个项目,在src上新建一个web service client ,填入 webservice的接口网址:类似于http://xx.xx.xx.xx:8080/newsso/ws/ssoService?wsdl

2.新建test类  直接测试

二、利用axis调用

package com.test;

import javax.xml.namespace.QName;

import net.sf.json.JSONObject;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class Test {
    public static void main(String[] args) {
        
        String endpoint = "http://xx.xx.xx.xx:8080/newsso/ws/ssoService?wsdl";   //接口的网址
        JSONObject jsonObject = new JSONObject();    
        try {
            Service service = new Service();
            //getsid 方法的测试
            Call call = (Call) service.createCall();
            call.setTargetEndpointAddress(endpoint);
            call.setOperationName(new QName("http://api.core.sso.fin.com/", "getsid"));//WSDL里面描述的接口名称
            call.addParameter("userid", org.apache.axis.encoding.XMLType.XSD_STRING,
            javax.xml.rpc.ParameterMode.IN);//接口的参数
            call.addParameter("sysid", org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);//接口的参数
            
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型 
            String userid  = "123";
            String sysid  = "tt";
            String result = (String)call.invoke(new Object[]{userid,sysid});
            //给方法传递参数,并且调用方法
            System.out.println("result is "+result);
            
            //getuserid方法的测试
            Call call1 = (Call) service .createCall();
            call1.setTargetEndpointAddress(endpoint);
            call1.setOperationName(new QName("http://api.core.sso.fin.com/", "getuser"));//WSDL里面描述的接口名称
            call1.addParameter("sid", org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);//接口的参数
            call1.addParameter("sysid", org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);//接口的参数
            call1.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型 
            //解析字符串
            jsonObject = jsonObject.fromString(result);
            String sid  = jsonObject.get("data").toString();    
            System.out.println(sid);
            result = (String)call1.invoke(new Object[]{sid,sysid});
            System.out.println("result is "+result);
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        } 
    }
}
 

猜你喜欢

转载自blog.csdn.net/KindergartenBoss/article/details/81905208