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>

     

 

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值