使用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();
        }
    }


}




發佈了35 篇原創文章 · 獲贊 12 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章