您的位置:首页 > 其它

CXF动态客户端调用webservice实例

2013-07-31 14:38 435 查看
使用CXF实现WebService,并在客户端实现动态调用编写服务器注意事项

注意 :不要指定

@SOAPBinding(style=Style.RPC, use=Use.LITERAL) 因为cxf 不支持:rpc、encoded,在动态客户调用过程。

cxf webservice开发资料,网上一搜大部分是类同的,跟官方的例子一样。

都是简单的静态调用例子。对动态调用的资料以及错误很少。本人被折腾了不行的情况下,

在一哥们的博客  http://hi.baidu.com/flierssp/item/2de0745c1afc1f3b32e0a96f 中看到动态调用不支持RPC才恍然大悟。

服务器端:

package com.rodjson.cxf;

import javax.jws.WebMethod;

import javax.jws.WebParam;

import javax.jws.WebService;

@WebService
/*@SOAPBinding(style=Style.RPC, use=Use.LITERAL) 注意 :如果要动态调用一定要注释掉这句代码*/

public interface HelloWorld {

 @WebMethod

 String sayHi(@WebParam(name="text") String text);

 }

package com.rodjson.cxf;

---HelloWorldImpl---

import javax.jws.WebService;

@WebService(endpointInterface="com.rodjson.cxf.HelloWorld",

  

  serviceName="HelloWorld" )

public class HelloWorldImpl implements HelloWorld {

   

           public String sayHi(String text) {

            System.out.println("sayHi called");    

            return "Hello " + text;

          }

         

}

部署:

package com.rodjson.cxf;

import javax.xml.ws.Endpoint;

public class webServiceApp {

 public static void main(String[] args) {

  System.out.println("web service start");

  HelloWorld implementor = new HelloWorldImpl();

  String address = "http://localhost:8080/helloWorld";

  Endpoint.publish(address, implementor);

  System.out.println("web service end");

 }

}

至于部署的方法网上很多,我就不提了,重点是动态客户端调用以及错误的识别

package com.rodjson.cxf;

import org.apache.cxf.endpoint.Client;

import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

public class ClientObj {

public static void main(String[] args) {

 JaxWsDynamicClientFactory  factory =JaxWsDynamicClientFactory.newInstance();

    Client client =factory.createClient("http://localhost:8080/cxf/services/HelloWorld?wsdl");

    try {

     Object[] obj =client.invoke("sayHi","xiao");

  System.out.println("resp:"+obj[0]);

 } catch (Exception e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

 }

}

}

错误如下:

2012-5-23 20:53:34 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames

信息: Created classes:

javacTask: 无源文件

用法: javacTask <options> <source files>

-help 用于列出可能的选项

2012-5-23 20:53:34 org.apache.cxf.endpoint.dynamic.DynamicClientFactory createClient

严重: Could not compile java files for http://127.0.0.1:9082/CrmWeb/services/CrmInterServer?wsdl.
如果觉得这篇文章对你有帮助,请不要吝啬你的鼠标,帮助下博主,

进博主开的程序员茶店:http://csxstea.taobao.com
凑个人气。万分感谢。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: