CXF 调用方式——动态创建客户端(调用稳定版本号为2.7.18)
客户端 方式 调用 18 稳定 版本号 2.7 动态创建
2023-09-11 14:19:27 时间
今天用动态创建客户端的方式调用webservice,报了这样一个错:
2017-01-05 20:51:46,029 DEBUG main org.apache.cxf.common.logging.LogUtils - Using org.apache.cxf.common.logging.Log4jLogger for logging. 2017-01-05 20:51:46,168 DEBUG main org.apache.cxf.endpoint.dynamic.DynamicClientFactory - Creating client from WSDL http://localhost/sdas/webService/TestWebservice?wsdl 2017-01-05 20:51:46,274 DEBUG main org.apache.cxf.transport.http.HTTPConduit - Conduit '{http://cxf.apache.org}TransportURIResolver.http-conduit' has been (re)configured for plain http. 2017-01-05 20:51:46,274 DEBUG main org.apache.cxf.transport.http.HTTPConduit - No Trust Decider configured for Conduit '{http://cxf.apache.org}TransportURIResolver.http-conduit' 2017-01-05 20:51:46,274 DEBUG main org.apache.cxf.transport.http.HTTPConduit - No Auth Supplier configured for Conduit '{http://cxf.apache.org}TransportURIResolver.http-conduit' 2017-01-05 20:51:46,274 DEBUG main org.apache.cxf.transport.http.HTTPConduit - Conduit '{http://cxf.apache.org}TransportURIResolver.http-conduit' has been configured for plain http. 2017-01-05 20:51:46,277 DEBUG main org.apache.cxf.transport.http.HTTPConduit - registering incoming observer: org.apache.cxf.transport.TransportURIResolver$1@6165e7a5 2017-01-05 20:51:46,292 DEBUG main org.apache.cxf.transport.http.Headers - Accept: */* 2017-01-05 20:51:46,294 DEBUG main org.apache.cxf.transport.http.TrustDecisionUtil - No Trust Decider for Conduit '{http://cxf.apache.org}TransportURIResolver.http-conduit'. An afirmative Trust Decision is assumed. 2017-01-05 20:51:46,481 DEBUG main org.apache.cxf.transport.http.HTTPConduit - Response Code: 200 Conduit: {http://cxf.apache.org}TransportURIResolver.http-conduit 2017-01-05 20:51:46,481 DEBUG main org.apache.cxf.transport.http.HTTPConduit - Content length: -1 2017-01-05 20:51:46,481 DEBUG main org.apache.cxf.transport.http.HTTPConduit - Header fields: null: [HTTP/1.1 200 OK] Date: [Thu, 05 Jan 2017 12:51:46 GMT] Transfer-Encoding: [chunked] Content-Type: [text/xml] Server: [Apache-Coyote/1.1] Exception in thread "main" java.lang.reflect.UndeclaredThrowableException at com.sun.proxy.$Proxy15.bind(Unknown Source) at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:318) at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:235) at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:228) at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:183) at platform.AnotherWSTest.main(AnotherWSTest.java:12) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.apache.cxf.common.util.ReflectionInvokationHandler.invoke(ReflectionInvokationHandler.java:53) ... 6 more Caused by: java.lang.NoSuchFieldError: theInstance at com.sun.tools.xjc.reader.xmlschema.BGMBuilder.<init>(BGMBuilder.java:165) at com.sun.tools.xjc.reader.xmlschema.BGMBuilder.build(BGMBuilder.java:112) at com.sun.tools.xjc.ModelLoader.annotateXMLSchema(ModelLoader.java:415) at com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl.bind(SchemaCompilerImpl.java:246) ... 11 more
服务端是没有问题的,用url调用可以正常执行。
最后通过更改我们的CXF版本解决了这个问题:
之前的版本:2.6.2
修改后的版本:2.7.18,试了用3.X.X表示不行
因为是用的maven所以该版本比较方便,如下:
常量
<properties> <cxf.version>2.7.18</cxf.version> </properties>
dependency
<!-- CXF WEBSERVICE --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <!-- CXF WEBSERVICE END -->
服务端和客户端最好统一,如果只改客户端(cxf-rt-frontend-jaxws)为2.7.18也是可以的
最后附一个动态调用示例写法:
import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import com.alibaba.fastjson.JSON; import javax.xml.bind.*; /** * @ClassName: WebServiceTest * @Description: TODO * @author: liuyx * @date: 2015年9月27日下午5:22:15 */ public class WebServiceTest { private static final String testUrl = "http://172.16.10.87/platform3.0/webService/TestWebservice?wsdl"; public static void main(String[] args) throws Exception { JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient(testUrl); Object[] objects=client.invoke("testOut", "test"); //输出调用结果 System.out.println(JSON.toJSONString(objects)); } }
相关文章
- 【Alljoyn】 Alljoyn学习笔记七 Alljoyn瘦客户端库介绍
- http协议客户端向服务器端请求时一般需要发送的内容
- 仿百度壁纸客户端(五)——实现搜索动画GestureDetector手势识别,动态更新搜索关键字
- 更精炼更专注的RTMPClient客户端EasyRTMPClient,满足直播、转发、分析等各种需求
- ActiveMq C#客户端 消息队列的使用(存和取)
- identity4 系列————纯js客户端案例篇[四]
- redis 简单整理——客户端常见异常[十七]
- docker客户端连接远程docker服务端(export方式)
- AsyncHttpClient使用示例:java http异步请求和websocket客户端
- Atitit 动态调用webservice与客户端代理方式调用
- 实时视频直播客户端技术盘点:Native、HTML5、WebRTC、微信小程序
- 干货:不同场景容器内获取客户端源IP的方法
- kafka客户端代码解析
- 爬虫日记(104):Twisted:客户端实现双向传送协议
- 【Zabbix实战之部署篇】Zabbix客户端的安装部署方法