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

CXF 动态调用webservice的错误

发布时间:2020-12-16 22:06:44 所属栏目:安全 来源:网络整理
导读:??????? String url = "http://localhost:8080/TestSpring/services/HelloWorld?wsdl"; ??????? JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory ??????????????? .newInstance(); ??????? Client client = factory.createClient(url); ????
??????? String url = "http://localhost:8080/TestSpring/services/HelloWorld?wsdl";
??????? JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory
??????????????? .newInstance();
??????? Client client = factory.createClient(url);
??????? Object[] res = client.invoke("sayHi","李晓辉");
??????? System.out.println(res[0]);
调用的代码就这么简单,但是却始终爆出如下额错误信息:
java.lang.NullPointerException
??????? at org.apache.cxf.common.util.Compiler.useJava6Compiler(Compiler.java:189)
??????? at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:143)
??????? at org.apache.cxf.common.util.Compiler.compileFiles(Compiler.java:138)
??????? at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.compileJavaSrc(DynamicClientFactory.java:598)
??????? at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:367)
??????? at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:270)
??????? at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:263)
??????? at org.apache.cxf.endpoint.dynamic.DynamicClientFactory.createClient(DynamicClientFactory.java:198)
??????? at main.java.demo.hw.client.Client.main(Client.java:43)

经过查看Compiler的代码,发现useJava6Compiler方法里面有这么一句“JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();”空指针的异常就是来自这里。产生的原因是没有获得编译环境,也就是JRE设置的问题,需要在eclipse里面把jre设置为jdk下的jre

参考:http://blog.sina.com.cn/s/blog_55fc875c01016x90.html

在CXF动态创建客户端时,如下:

JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();
Client client = null;
// 创建客户端连接
client = factory.createClient("http://localhost:9090/cxf/saveLove?wsdl");

经测试,在createClient操作中,会生产webservice下所有方法类、参数类、返回值类等等文件,耗时相当惊人,超级慢,后来想要需要优化创建初始化步骤;

后来从博友http://ruijf.iteye.com/blog/1186961上看到创建的client是线性安全,正好是自己所想要的效果;

参考:http://pangsir.iteye.com/blog/1492515

public class HttpClientUtil {

	private static Map<String,Client> clientMap = new HashMap<String,Client>(); 

		/**
		 * 调用wms系统的方法
		 * @param serviceURL
		 * @param wareHouseCode
		 * @param reqXmlBody
		 * @return
		 * @throws Exception
		 */
		public static String invokeWebService(String serviceURL,String wareHouseCode,String reqXmlBody) throws Exception{
			Client client = (Client) clientMap.get(serviceURL);
			if(client==null){
				JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
				client = dcf.createClient(serviceURL);
				clientMap.put(serviceURL,client);
			}
			
			Object[] result = client.invoke("receiveReOrder",wareHouseCode,reqXmlBody);
			
			if(result!=null){
				return result[0].toString();
			}
			return null;
		}
}

(编辑:李大同)

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

    推荐文章
      热点阅读