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

CXF动态客户端调用webservice实例

发布时间:2020-12-16 23:02:23 所属栏目:安全 来源:网络整理
导读:使用CXF实现WebService,并在客户端实现动态调用编写服务器注意事项 注意 :不要指定 @SOAPBinding(style=Style.RPC,use=Use.LITERAL) 因为cxf 不支持:rpc、encoded,在动态客户调用过程。 cxf webservice开发资料,网上一搜大部分是类同的,跟官方的例子一

使用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.

(编辑:李大同)

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

    推荐文章
      热点阅读