使用axis2解析wsdl

1.使用rpc方式解析远程wsdl:

优点在于:代码简单、不用繁琐的生成服务端的一大堆代码

缺点在于:需要知道要解析的wsdl的服务端部分的实体类的定义


2.使用的包的maven依赖:

 <!--==============axis2 client===============-->
        <!--http://blog.csdn.net/liwf_/article/details/9788789-->
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2</artifactId>
            <version>${axis2.version}</version>
            <type>pom</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-kernel</artifactId>
            <version>${axis2.version}</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-adb</artifactId>
            <version>${axis2.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-http</artifactId>
            <version>${axis2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.axis2</groupId>
            <artifactId>axis2-transport-local</artifactId>
            <version>${axis2.version}</version>
            <exclusions>
                <!-- We want to choose the JavaMail implementation ourselves -->
                <exclusion>
                    <groupId>javax.mail</groupId>
                    <artifactId>mail</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--http://dominikdorn.com/2010/05/maven-junit-classformaterror-absent-code-attribute/-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4</version>
            <scope>provided</scope>
        </dependency>
        

3.工具类代码:

public class CallWSDLByAxis2 {


    /**
     * RPC调用AXIS2 webservice
     *
     * @param endpoint 服务地址
     * 如:http://192.168.0.1:2597/aixs2/services/jqservice?wsdl
     * @param localPart 方法名 如<xs:element name="Receive">
     * @param opArgs 方法参数 如Object[] opArgs = new Object[] { param };
     * @param namespaceURI 命名空间 如 :targetNamespace="http://server.test.com.cn">
     * @param opReturnType 返回类型 如字符串:Class[] opReturnType = new Class[] {
     * String[].class };
     * @param timeoutSeconds
     * @param username
     * @param password
     * @return
     */
    public static Object axis2RPCInvoke(String endpoint, String localPart, Object[] opArgs, String namespaceURI, Class[] opReturnType, int timeoutSeconds, String username, String password) {
        Object[] ret = null;
        try {
            RPCServiceClient serviceClient = new RPCServiceClient();


            Options options = serviceClient.getOptions();


            Authenticator authenticator = new Authenticator();
            List<String> auth = new ArrayList<String>();
            auth.add(Authenticator.BASIC);
            authenticator.setAuthSchemes(auth);
            authenticator.setUsername(username);
            authenticator.setPassword(password);
            authenticator.setPreemptiveAuthentication(true);
            options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);
            options.setTimeOutInMilliSeconds(timeoutSeconds * 1000);
            EndpointReference targetEPR = new EndpointReference(endpoint);
            options.setTo(targetEPR);
            QName opQName = new QName(namespaceURI, localPart);
            ret = serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
            serviceClient.cleanupTransport();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret[0];
    }


    /**
     * 没有返回值的请求
     * @param endpoint
     * @param localPart
     * @param opArgs
     * @param namespaceURI
     * @param timeoutSeconds
     * @param username
     * @param password 
     */
    public static void axis2RPCInvoke(String endpoint, String localPart, Object[] opArgs, String namespaceURI, int timeoutSeconds, String username, String password) {
        try {
            RPCServiceClient serviceClient = new RPCServiceClient();


            Options options = serviceClient.getOptions();


            Authenticator authenticator = new Authenticator();
            List<String> auth = new ArrayList<String>();
            auth.add(Authenticator.BASIC);
            authenticator.setAuthSchemes(auth);
            authenticator.setUsername(username);
            authenticator.setPassword(password);
            authenticator.setPreemptiveAuthentication(true);
            options.setProperty(HTTPConstants.AUTHENTICATE, authenticator);
            options.setProperty(HTTPConstants.CHUNKED, false);
            options.setProperty(Constants.Configuration.MESSAGE_TYPE, HTTPConstants.MEDIA_TYPE_APPLICATION_ECHO_XML);
            options.setProperty(Constants.Configuration.DISABLE_SOAP_ACTION, Boolean.TRUE);
            options.setTimeOutInMilliSeconds(timeoutSeconds * 1000);


            EndpointReference targetEPR = new EndpointReference(endpoint);
            options.setTo(targetEPR);
            QName opQName = new QName(namespaceURI, localPart);
            serviceClient.invokeRobust(opQName, opArgs);
            serviceClient.cleanupTransport();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值