java調用webservice接口
java怎麼調用webservice接口呢?不知道的小夥伴來看看小編今天的分享吧!
java調用webservice接口有三種方法。
方法一:直接AXIS調用遠程的web service,輸入代碼:
public void doSelectRiskReportForm(HttpServletRequest request,
HttpServletResponse response){
//調用接口
//方法一:直接AXIS調用遠程的web service
try {
String endpoint = "http://localhost:8080/platform-jxcx-service/services/settlementServiceImpl?wsdl";
Service service = new Service();
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
String parametersName = "settle_num"; // 參數名//對應的是 public String printWord(@WebParam(name = "settle_num") String settle_num);
//
call.setOperationName("printWord"); // 調用的方法名//當這種調用不到的時候,可以使用下面的,加入命名空間名
call.setOperationName(new QName("http://jjxg_settlement.platform.bocins.com/", "printWord"));// 調用的方法名
call.addParameter(parametersName, XMLType.XSD_STRING, ParameterMode.IN);//參數名//XSD_STRING:String類型//.IN入參
call.setReturnType(XMLType.XSD_STRING); // 返回值類型:String
String message = "123456789";
String result = (String) call.invoke(new Object[] { message });// 遠程調用
System.out.println("result is " + result);
} catch (Exception e) {
System.err.println(e.toString());
}
}
方法二:直接SOAP調用遠程的webservice
下載jar,SOAP 使用 HTTP 傳送 XML,儘管HTTP 不是有效率的通訊協議,而且 XML 還需要額外的文件解析(parse),兩者使得交易的速度大大低於其它方案。但是XML 是一個開放、健全、有語義的訊息機制,而 HTTP 是一個廣泛又能避免許多關於防火牆的問題,從而使SOAP得到了廣泛的應用。但是如果效率對你來説很重要,那麼你應該多考慮其它的方式,而不要用 SOAP。
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();
}
}
}
方法三:直接使用eclipse生成客户端.idea類同
以天氣預報的為例:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
用編輯器打開下載的文件,將
<s:element ref="s:schema" /> <s:any />
替換成
<s:any minOccurs="2" maxOccurs="2" />
然後將文件另存為weather.wsdl。
打開保存的文件路徑輸入cmd,輸入
wsimport -s . weather.wsdl
顯示以上內容,即為生成成功,以下這是生成的文件
新建一個測試類WebserviceTest .java:
public class WebserviceTest {
public static void main(String[] args) {
//也可以使用new WeatherWebService(url)此方法可重新設置請求的地址 URL url=new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl")
WeatherWebService factory = new WeatherWebService();
WeatherWebServiceSoap weatherWebServiceSoap = factory.getWeatherWebServiceSoap(); //WeatherWebServiceSoap為調用的實現類
ArrayOfString strArray = null;
strArray = weatherWebServiceSoap.getWeatherbyCityName("武漢");
System.out.println(strArray.getString());
}
}
顯示以下內容 即為調用成功。