CXFf客戶端依賴服務器和不依賴服務器的兩種實現方式

package com.ws.cxf.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
public class Client {
    public static void main(String[] args) {
                       
          //**********依賴服務器端*****************
           /*
            * //創建WebService客戶端代理工廠
            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            // 註冊WebService接口
            factory.setServiceClass(IHelloWorld.class);
            // 設置WebService地址
            factory.setAddress("http://localhost:8080/cxfTest/webservice/HelloWorld");
            IHelloWorld iHelloWorld = (IHelloWorld) factory.create();
            System.out.println(iHelloWorld.sayHello("jim"));
            */
                       
         //**********不依賴服務器端*****************
        JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
        org.apache.cxf.endpoint.Client client = clientFactory.createClient("http://localhost:8080/cxfTest/webservice/HelloWorld?wsdl");
        try {
            Object[] result = client.invoke("sayHello", "jim");//invoke(方法名,參數)
            System.out.println(result[0]);
            System.exit(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
                       
    }
}

注意:

當不依賴服務器端時,接口的實現類必須在@WebService中加上表空間,否則會報異常:

org.apache.cxf.common.i18n.UncheckedException: No operation was found with the name {http://daoImpl.cxf.ws.com/}sayHello.
   at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:331)
   at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:325)
   at com.ws.cxf.client.Client.main(Client.java:25)

接口實現如下

package com.ws.cxf.daoImpl;
import javax.jws.WebService;
import com.ws.cxf.dao.IHelloWorld;
@WebService(endpointInterface="com.ws.cxf.dao.IHelloWorld",
        serviceName="helloWorld",
        targetNamespace="http://dao.cxf.ws.com/")
public class HelloWorldImpl implements IHelloWorld{
    public String sayHello(String username) {
        System.out.println("sayHello() is called");
        return username +" helloWorld";
    }
}
  1. targetNamespace="http://dao.cxf.ws.com/" 爲名稱空間,  

  2. 指定從 Web Service 生成的 WSDL 和 XML 元素的 XML 名稱空間。  

  3. 缺省值爲從包含該 Web Service 的包名映射的名稱空間。(字符串)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章