String url = "http://localhost:8080/TestSpring/services/HelloWorld?wsdl"; JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory .newInstance(); Client client = factory.createClient(url); Object[] res = client.invoke("sayHi", "李晓辉
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; } }