Axis2遠程調用WebService接口

測試訪問路徑爲 http://localhost:8080/axis2/services/Version 的WebService服務

  1. 先確認webservice服務是可以正常訪問的,可通過SOAPUI工具進行測試
  2. 查看webservice服務的wsdl文件,直接在WebService服務路徑上添加後綴 “?wsdl”
  3. 獲取域名domain(wsdl:definitions標籤下的targetNamespace屬性值)
  4. 獲取要訪問的接口(方法)名稱(wsdl:binding標籤下wsdl:operation的name屬性值)
  5. 獲取要訪問的接口的參數名及參數類型(sequence標籤下的element標籤)
  6. 獲取接口(方法)的訪問路徑(wsdl:binding標籤下wsdl:operation標籤下soap:operation的soapAction的屬性值)
  7. 編寫測試代碼
    package version;
    
    import org.apache.axiom.om.OMAbstractFactory;
    import org.apache.axiom.om.OMElement;
    import org.apache.axiom.om.OMFactory;
    import org.apache.axiom.om.OMNamespace;
    import org.apache.axiom.soap.SOAP11Constants;
    import org.apache.axiom.soap.SOAP12Constants;
    import org.apache.axis2.AxisFault;
    import org.apache.axis2.Constants;
    import org.apache.axis2.addressing.EndpointReference;
    import org.apache.axis2.client.Options;
    import org.apache.axis2.client.ServiceClient;
    import org.apache.axis2.transport.http.HTTPConstants;
    
    import java.rmi.RemoteException;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    public class VersionTest {
        public static void main(String[] args) {
            invokeClientService();
        }
    
        private static void invokeClientService() {
            // 服務路徑
            final String serviceUrl = "http://localhost:8080/axis2/services/Version";
            // 域名
            final String domain = "http://axisversion.sample";
            final String action = "urn:getVersion";
            final String methodName = "getVersion";
            try {
                // 設置客戶端請求參數
                final Options options = new Options();
                options.setAction(action);
                final EndpointReference endpointReference = new EndpointReference(serviceUrl);
                options.setTo(endpointReference);
    
                // 設置使用Soap協議的版本
                options.setSoapVersionURI(SOAP12Constants.SOAP_ENVELOPE_NAMESPACE_URI);
                //options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
    
                // 設置傳輸協議
                options.setTransportInProtocol(Constants.TRANSPORT_HTTP);
                // 禁用分塊支持
                options.setProperty(HTTPConstants.CHUNKED, false);
    
                final ServiceClient serviceClient = new ServiceClient();
                serviceClient.setOptions(options);
    
                // 設置參數
                final Map<String, String> params = new LinkedHashMap<>();
    
                final OMElement method = buildRequestMethod(domain, methodName, params);
                method.build();
    
                final OMElement result = serviceClient.sendReceive(method);
                System.out.println(result.toString());
                System.out.println("result : " + result.getFirstElement().getText());
            } catch (AxisFault axisFault) {
                axisFault.printStackTrace();
            }
        }
    
        private static OMElement buildRequestMethod(final String domain, final String methodName, final Map<String, String> params) {
            // 調用方法設置參數
            final OMFactory omFactory = OMAbstractFactory.getOMFactory();
            final OMNamespace omNamespace = omFactory.createOMNamespace(domain, "");
            final OMElement method = omFactory.createOMElement(methodName, omNamespace);
    
            params.forEach((paramName, paramValue) -> {
                final OMElement param = omFactory.createOMElement(paramName, omNamespace);
                param.setText(paramValue);
                // 參數的添加順序必須和接口中參數的順序一致
                method.addChild(param);
            });
            return method;
        }
    }
    

     

  8. 測試結果
  9. axis2依賴的jar包
    <dependencies>
    
            <!-- webservice 遠程調用接口 -->
            <dependency>
                <groupId>org.apache.axis2</groupId>
                <artifactId>axis2</artifactId>
                <version>1.7.9</version>
                <type>pom</type>
            </dependency>
            <dependency>
                <groupId>org.apache.axis2</groupId>
                <artifactId>axis2-transport-http</artifactId>
                <version>1.7.9</version>
            </dependency>
            <dependency>
                <groupId>org.apache.axis2</groupId>
                <artifactId>axis2-spring</artifactId>
                <version>1.7.9</version>
            </dependency>
            <dependency>
                <groupId>org.apache.axis2</groupId>
                <artifactId>axis2-transport-local</artifactId>
                <version>1.7.9</version>
            </dependency>
            <dependency>
                <groupId>org.apache.axis2</groupId>
                <artifactId>axis2-kernel</artifactId>
                <version>1.7.9</version>
            </dependency>
            <dependency>
                <groupId>org.apache.axis2</groupId>
                <artifactId>axis2-adb</artifactId>
                <version>1.7.9</version>
            </dependency>
            <dependency>
                <groupId>org.apache.xmlbeans</groupId>
                <artifactId>xmlbeans</artifactId>
                <version>3.1.0</version>
            </dependency>
    
            <!-- https://mvnrepository.com/artifact/org.apache.axis2/addressing -->
            <dependency>
                <groupId>org.apache.axis2</groupId>
                <artifactId>addressing</artifactId>
                <version>1.7.9</version>
            </dependency>
    
        </dependencies>

     

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章