本文目錄一覽:
resttemplate遠程接口調用,傳一個map怎麼調用map參數?
spring rest mvc使用RestTemplate遠程接口調用
主要代碼如下:
import java.util.HashMap;
import java.util.Map;
import org.springframework.web.client.RestTemplate;
參數直接放在URL中 String message =restTemplate.getForObjectocalhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijieid=80″,Str參數使用MAP傳遞
MapString ,Object urlVariables = new HashMapString,Object();
urlVariables.put(“name”, “zhaoshijie”);
urlVariables.put(“id”, 80);
String message2restTemplate.getForObject
delete方法沒有返回值,說明,id=0這個參數在服務器端可以不定義該參數,直接使用request獲取
restTemplate.putyongbarservice/appstore/appgoods/restTemplate?name=zhaoshijieid=80″,null);
System.out.println(message);
System.out.println(message2);
System.out.println(message3); }
java中接口傳參問題
先從後面回答吧,後一個簡單
如果不進行mOnSelectListener = onSelectListener;
那麼你在圖一的最上面一行mOnSelectListener.getValue的調用時,會遭到空指針異常。對於成員變量,初始值是null,這個我想你不會忘記吧?
第一個問題不太懂,這裡有什麼疑問么?接口規定了兩個String參數,你只要傳的是String就沒問題,不明白你的疑問在哪裡?
java怎麼遠程調用webservice接口
Java通過WSDL文件來調用webservice:
注意,以下的代碼並沒有經過真正的測試,只是說明這些情況,不同版本的Axis相差很大,大家最好以apache網站上的例子為準,這裡僅僅用於說明其基本用法。
1,直接AXIS調用遠程的web service
這種方法比較適合那些高手,他們能直接看懂XML格式的WSDL文件,我自己是看不懂的,尤其我不是專門搞這行的,即使一段時間看懂,後來也就忘記了。直接調用模式如下:
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 = “”;
//直接引用遠程的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());
}
}
}
2,直接SOAP調用遠程的webservice
這種模式我從來沒有見過,也沒有試過,但是網絡上有人貼出來,我也轉過來
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(“”);
} 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文件轉成本地類,然後像本地類一樣使用,即可。
這是像我這種懶人最喜歡的方式,仍然以前面的global weather report為例。
首先 java org.apache.axis.wsdl.WSDL2Java
原本的網址是,中間個各問號,但是Linux下面它不能解析,所以去掉問號,改為點號。
那麼就會出現4個文件:
GlobalWeather.java GlobalWeatherLocator.java GlobalWeatherSoap.java GlobalWeatherSoapStub.java
其中GlobalWeatherSoap.java是我們最為關心的接口文件,如果你對RMI等SOAP實現的具體細節不感興趣,那麼你只需要看接口文件即可,在使用的時候,引入這個接口即可,就好像使用本地類一樣。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/243808.html