使用cxf調用webservice

一、引入maven包apache

<dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>${cxf.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>${cxf.version}</version>
        </dependency>
View Code

二、建立Client對象frontend

public static Client getDynamicClient(String wsdlUrl) {
        if (client == null) {
            synchronized (this.class) {
                if (client == null) {
                    DynamicClientFactory dcf = DynamicClientFactory.newInstance();
                    client = dcf.createClient(wsdlUrl);
                }
            }
        }
        return client;
    }
View Code

三、請求遠程方法maven

    private String getResult(String reqXml, String wsdlUrl, String method) {
        String result = null;
        Object[] objects = null;
        try {
            LOGGER.debug("reqXml={}.", reqXml);
            objects = getDynamicClient(wsdlUrl).invoke(method, reqXml);
            result = (String) objects[0];
            LOGGER.debug("GbotExecutor.getResult response={}.", result);
        }
        catch (Exception e) {
            LOGGER.error("invoke gbot getResult failed!", e);
        }
        return result;
    }
View Code
相關文章
相關標籤/搜索