java調(diào)用webservice接口 幾種方法

2022-05-27    分類: 網(wǎng)站建設(shè)

webservice的 發(fā)布一般都是使用WSDL(web service descriptive language)文件的樣式來發(fā)布的,在WSDL文件里面,包含這個webservice暴露在外面可供使用的接口。

下面我們來看Java如何通過WSDL文件來調(diào)用這些web service:

注意,以下的代碼并沒有經(jīng)過真正的測試,只是說明這些情況,不同版本的Axis相差很大,大家最好以apache網(wǎng)站上的例子為準(zhǔn),這里僅僅用于說明其基本用法。

1,直接AXIS調(diào)用遠(yuǎn)程的web service
我覺得這種方法比較適合那些高手,他們能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是專門搞這行的,即使一段時間看懂,后來也就忘記了。直接調(diào)用模式如下:

[java] view plain copy
import 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";
// 直接引用遠(yuǎn)程的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);// 接口的參數(shù)
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 設(shè)置返回類型
String temp = "測試人員";
String result = (String) call.invoke(new Object[] { temp });
// 給方法傳遞參數(shù),并且調(diào)用方法
System.out.println("result is " + result);
} catch (Exception e) {
System.err.println(e.toString());
}
}
}

2,直接SOAP調(diào)用遠(yuǎn)程的webservice

[java] view plain copy
import 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();
}
}
}

3,使用wsdl2java把WSDL文件轉(zhuǎn)成本地類,然后像本地類一樣使用,即可。

這是像我這種懶人最喜歡的方式,仍然以前面的global weather report為例。
首先 java org.apache.axis.wsdl.WSDL2Java http://www.webservicex.net/globalweather.asmx.WSDL

原本的網(wǎng)址是http://www.webservicex.net/globalweather.asmx?WSDL,中間個各問號,但是Linux下面它不能解析,所以去掉問號,改為點號。


那么就會出現(xiàn)4個文件:

GlobalWeather.java 

GlobalWeatherLocator.java

GlobalWeatherSoap.java

GlobalWeatherSoapStub.java


其中GlobalWeatherSoap.java是我們最為關(guān)心的接口文件,如果你對RMI等SOAP實現(xiàn)的具體細(xì)節(jié)不感興趣,那么你只需要看接口文件即可,在使用的時候,引入這個接口即可,就好像使用本地類一樣。

本文名稱:java調(diào)用webservice接口 幾種方法
當(dāng)前URL:http://www.bm7419.com/news35/159735.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、建站公司、小程序開發(fā)、域名注冊、關(guān)鍵詞優(yōu)化、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名