java js webservice接口_java调用webservice接口实现方式有哪些?

本文详细介绍了如何在Java环境中通过AXIS和SOAP两种方式调用WebService。首先,展示了使用AXIS调用WebService的步骤,包括设置目标地址、操作名称和参数。接着,解释了SOAP调用过程,涉及SOAP对象创建、参数设置和方法调用。通过实例代码,读者可以了解这两种方法的具体实现,以便于在实际开发中应用。
摘要由CSDN通过智能技术生成

我们知道,接口是java的一个重要概念,一切的实现都基于接口,这次,小编要为大家讲解的就是如何在java中调用webservice,下面就一下来看看。

一、AXIS远程调用web serviceimport java.util.Date;

import java.text.DateFormat;

import org.apache.axis.client.Call;

import org.apache.axis.client.Service;

import javax.xml.namespace.QName;

import java.lang.Integer;

import javax.xml.rpc.ParameterMode;

public class caClient

{

public static void main(String[] args)

{

try

{

String endpoint = "http://localhost:8080/ca3/services/caSynrochnized?wsdl";

// 直接引用远程的wsdl文件

// 以下都是套路

Service service = new Service();

Call call = (Call) service.createCall();

call.setTargetEndpointAddress(endpoint);

call.setOperationName("addUser"); // WSDL里面描述的接口名称

call.addParameter("userName"

, org.apache.axis.encoding.XMLType.XSD_DATE

, javax.xml.rpc.ParameterMode.IN); // 接口的参数

call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); // 设置返回类型

String temp = "测试人员";

String result = (String) call.invoke(new Object[]

{

temp

});

// 给方法传递参数,并且调用方法

System.out.println("result is " + result);

}

catch (Exception e)

{

System.err.println(e.toString());

}

}

}

二、SOAP远程调用webserviceimport org.apache.soap.util.xml.*;

import org.apache.soap.*;

import org.apache.soap.rpc.*;

import java.io.*;

import java.net.*;

import java.util.Vector;

public class caService

{

public static String getService(String user)

{

URL url = null;

try

{

url = new URL(

"http://192.168.0.100:8080/ca3/services/caSynrochnized");

}

catch (MalformedURLException mue)

{

return mue.getMessage();

}

// This is the main SOAP object

Call soapCall = new Call();

// Use SOAP encoding

soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);

// This is the remote object we're asking for the price

soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");

// This is the name of the method on the above object

soapCall.setMethodName("getUser");

// We need to send the ISBN number as an input parameter to the method

Vector soapParams = new Vector();

// name, type, value, encoding style

Parameter isbnParam = new Parameter("userName", String.class, user

, null);

soapParams.addElement(isbnParam);

soapCall.setParams(soapParams);

try

{

// Invoke the remote method on the object

Response soapResponse = soapCall.invoke(url, "");

// Check to see if there is an error, return "N/A"

if (soapResponse.generatedFault())

{

Fault fault = soapResponse.getFault();

String f = fault.getFaultString();

return f;

}

else

{

// read result

Parameter soapResult = soapResponse.getReturnValue();

// get a string from the result

return soapResult.getValue()

.toString();

}

}

catch (SOAPException se)

{

return se.getMessage();

}

}

}

以上就是本篇文章的所有内容,更多java常见问题及解决方法敬请关注奇Q工具网了解详情。

推荐阅读:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值