一、Axis簡介
Apache Axis是一個基於Java的開源Web Services框架,通過使用SOAP(簡單對象訪問協議)消息來傳遞和接收調用請求和調用結果。Axis提供了一個可擴展、標準的框架,可以讓利用SOAP協議的Web服務進行互操作,同時支持WSDL(Web Services Description Language)描述和UDDI(Universal Description, Discovery and Integration)註冊。
二、如何創建和調用webservice介面
我們可以通過以下步驟來創建和調用webservice介面:
1、使用Java EE IDE或者手動創建一個Java Web項目。
2、編寫WebService介面與方法,使用@WebService和@WebMethod註解來標註WebService和方法。
下面是一個WebService介面的示例:
package org.example.webservice;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
@WebMethod
String sayHello(String name);
}
3、編寫實現類,並用@WebService(endpointInterface=”包名+介面名”)註解標註WebService類。
下面是一個WebService實現類的示例:
package org.example.webservice;
import javax.jws.WebService;
@WebService(endpointInterface = "org.example.webservice.HelloWorld")
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {
return "Hello " + name;
}
}
4、將WebService發布到Tomcat伺服器中。
下面是發布WebService的示例:
package org.example.webservice;
import javax.xml.ws.Endpoint;
public class AxisWebServicePublisher {
public static void main(String[] args) {
String address = "http://localhost:8080/axis/helloworld";
Endpoint.publish(address, new HelloWorldImpl());
System.out.println("WebService started @ " + address);
}
}
5、調用WebService介面。
可以使用Axis提供的工具生成客戶端代碼,包括Java、C#、Python等。下面是生成Java客戶端代碼的示例:
wsdl2java.bat -uri http://localhost:8080/axis/helloworld?wsdl -d . -p org.example.webservice.client
生成的Java代碼可以通過Java EE IDE或者命令行的方式來進行編譯和運行。下面是Java客戶端的示例代碼:
package org.example.webservice.client;
import org.example.webservice.HelloWorld;
import org.example.webservice.HelloWorldImplService;
public class AxisWebServiceClient {
public static void main(String[] args) {
HelloWorldImplService service = new HelloWorldImplService();
HelloWorld port = service.getHelloWorldImplPort();
String response = port.sayHello("World");
System.out.println(response);
}
}
三、Axis調用webservice介面的更多相關知識
1、如何指定超時時間
可以通過以下方式來指定請求超時時間:
String address = "http://localhost:8080/axis/helloworld";
HelloWorldImpl helloWorld = new HelloWorldImpl();
// 設置請求超時時間為30秒
((BindingProvider) helloWorld).getRequestContext().put("javax.xml.ws.client.timeout", 30000);
Endpoint.publish(address, helloWorld);
2、如何指定連接超時時間
可以通過以下方式來指定連接超時時間:
String address = "http://localhost:8080/axis/helloworld";
HelloWorldImpl helloWorld = new HelloWorldImpl();
// 設置連接超時時間為10秒
Map<String, Object> map = ((BindingProvider) helloWorld).getRequestContext();
map.put("javax.xml.ws.client.connectionTimeout", 10000);
Endpoint.publish(address, helloWorld);
3、如何處理異常信息
在調用webservice介面時,可能會遇到一些異常,需要進行處理。下面是處理異常的示例代碼:
try {
String response = port.sayHello("World");
System.out.println(response);
} catch (WebServiceException e) {
System.err.println("WebServiceException: " + e.getMessage());
}
4、如何使用SOAP頭部信息
使用SOAP頭部信息可以為webservice請求添加自定義的授權信息,下面是一個使用SOAP頭部信息的示例代碼:
QName qName = new QName("http://example.org/metadata", "id");
SOAPHeaderElement header = header = new SOAPHeaderElement(qName);
header.setTextContent("123456");
List<Header> headers = new ArrayList<Header>();
headers.add(header);
((BindingProvider) port).getRequestContext().put(Header.HEADER_LIST, headers);
String response = port.sayHello("World");
System.out.println(response);
四、總結
本文詳細介紹了Axis調用webservice介面的過程,包括創建和發布webservice,以及調用webservice介面時可能遇到的問題和解決方法。通過本文的學習,可以更好地理解Axis的使用方法和調用webservice介面的流程。
原創文章,作者:QXWJ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/142058.html