java调用webService办法
家电维修 2023-07-16 19:16www.caominkang.com家电维修技术
1.pom依赖
.apache.cxf cxf-rt-transports-http3.1.8 .apache.cxf cxf-rt-frontend-jaxs3.1.8
2.新建重新编码的java文件JAXDynamicClientFactory
package .emr.controller; import .apache.cxf.Bus; import .apache.cxf.bus.CXFBusFactory; import .apache.cxf.endpoint.EndpointImplFactory; import .apache.cxf.endpoint.dynamic.DynamicClientFactory; import .apache.cxf.jaxs.support.JaxWsEndpointImplFactory; import java.util.List; public class JAXDynamicClientFactory extends DynamicClientFactory { protected JAXDynamicClientFactory(Bus bus) { super(bus); } protected EndpointImplFactory getEndpointImplFactory() { return JaxWsEndpointImplFactory.getSingleton(); } protected boolean alloWrapperOps() { return true; } public static JAXDynamicClientFactory neInstance(Bus b) { return ne JAXDynamicClientFactory(b); } public static JAXDynamicClientFactory neInstance() { Bus bus = CXFBusFactory.getThreadDefaultBus(); return ne JAXDynamicClientFactory(bus); } public boolean pileJavaSrc(String classPath, List srcList, String dest) { .apache.cxf.mon.util.Compiler javaCompiler = ne .apache.cxf.mon.util.Compiler(); javaCompiler.setEncoding("UTF-8"); javaCompiler.setClassPath(classPath); javaCompiler.setOutputDir(dest); javaCompiler.setTarget("1.8"); return javaCompiler.pileFiles(srcList); } }
3.调用
//创建连接工厂 JAXDynamicClientFactory dcf = JAXDynamicClientFactory.neInstance(); //创建客户端 sUrl为ebService接口地址 Client client = dcf.createClient(sUrl); //动态调用方法,sMethod为调用ebService接口的方法名,params是参数名称,支持多个 Object[] objects = client.invoke(sMethod,params...); //销毁连接 client.destroy(); //接收结果 String result = objects[0].toString(); System.out.println("result:"+result);