CXF調用方式——使用wsdl2java(Windwos下)


1、自動生成客戶端代碼:

先把CXF下到本地,本例中我下的是apache-cxf-3.1.2,然后在命令行里到相應路徑執行命令:

D:\soft\DevelopSoft\apache-cxf-3.1.2\bin>wsdl2java -d D:\\src -client http://172.16.10.87/platform3.0/webService/TestWebservice?wsdl

 D:\soft\DevelopSoft\apache-cxf-3.1.2\bin>wsdl2java -encoding utf-8 -d D:\\src -client http://localhost:8080/dbws?wsdl

如此wsdl2java工具會自動為你生成客戶端可調用代碼在D盤的src目錄下。

 

2、在程序中使用

本例中服務端有一個接口ITestWebservice以及一個實現類TestWebserviceImpl。

自動生成的代碼里會有一個ITestWebservice以及一個TestWebserviceImplService,與上邊兩個算是一個對應關系。

使用的時候有兩種方式:

一、可以只通過ITestWebservice加地址的方式來調用

二、也可以通過ITestWebserviceTestWebserviceImplService的方式來調用

下邊的代碼里包含了這兩種方式:

import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.sdyy.webservice.ITestWebservice;
import com.sdyy.webservice.TestWebserviceImplService;

/**
 * @ClassName: WebServiceTest
 * @Description: TODO
 * @author: liuyx 
 * @date: 2015年9月27日下午5:22:15
 */
public class WebServiceTest3 {
    private static final String testUrl = "http://172.16.10.87/platform3.0/webService/TestWebservice?wsdl";
    public static void main1(String[] args) throws Exception {
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
        factoryBean.getInInterceptors().add(new LoggingInInterceptor());
            factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
            factoryBean.setServiceClass(ITestWebservice.class);
            factoryBean.setAddress(testUrl);
            ITestWebservice impl = (ITestWebservice) factoryBean.create();
            System.out.println(impl.testOut("咯咯咯"));
    }
    public static void main(String[] args) throws Exception {
        TestWebserviceImplService factory = new TestWebserviceImplService();
        ITestWebservice testOut = factory.getTestWebserviceImplPort();
        System.out.println(testOut.testOut("GEGEGE"));
    }
}

 

 

動態創建客戶端的方式,請參閱本博另一篇。

http://www.cnblogs.com/flying607/p/6254045.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM