加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

CXFf客户端依赖服务器和不依赖服务器的两种实现方式

发布时间:2020-12-13 23:02:35 所属栏目:百科 来源:网络整理
导读: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) { //**********依赖服务器端*****
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 的包名映射的名称空间。(字符串)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读