两种方法: 一,使用jdk自带工具根据服务器地址,生成服务实体类接口 1,eclipse中新建一个项目, 2,使用jdk自带wsimport工具自动生成服务端java代码,打开dos窗口,输入 wsimport -keephttp:
两种方法:
一,使用jdk自带工具根据服务器地址,生成服务实体类接口
1,eclipse中新建一个项目,
2,使用jdk自带wsimport工具自动生成服务端java代码,打开dos窗口,输入 wsimport -keep http://10.23.11.1:8080/webService/buildingEngService?wsdl
服务端java 代码回自动生成,在当前目录下,只复制两个文件即可。一个是接口BuildingEngService,一个是BuildingEngService_Service,
3,把生成的两个文件代码复制到项目中,
4,新建一个java测试类HelloService。
package com.je.ws; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import cn.com.guodiantong.pb.webservice.server.base.service.BuildingEngService; public class HelloService { public static void main(String[] args) { Service energyService; Class energyTargetInterface = BuildingEngService.class; String url = "http://10.113.11.1:9090/enocpService/buildingEngService"; String targetNameSpace = "http://service.base.server.webservice.pb.guodiantong.com.cn/"; String interService = "buildingEngService"; String targetServicePort = "BuildingEngServicePort"; try { energyService = Service.create(new URL(url),new QName(targetNameSpace, interService)); QName energyQname = new QName(targetNameSpace,targetServicePort); BuildingEngService energyPort = energyService.getPort(energyQname, energyTargetInterface); String json = "{\"inputFlag\":{\"systemCode\":\"BENX\",\"functionCode\":\"getElectricRecording\"},\"inputData\":[{\"deviceNumber\":\"T1001\",\"electricQuant ity\":\"9784065.00\",\"electricLoad\":\"2349.90\",\"collectTime\":\"2016-01-11 14:30:00\"}]}"; String re = energyPort.getElectricRecording(json); System.out.println("result == "+ re); } catch (MalformedURLException e) { e.printStackTrace(); } } }
5,Run as java Application 完成。
二,使用axis2生成客户端调用服务端****************************************************************************************************
1,eclipse新建一个工程
2,src下新建一个类:
package main; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class Axis2ClientTest { public static void main(String[] args) { RPCServiceClient serviceClient = null; try { serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference("http://192.168.25.95:8080/emplatform/services/quZhouService"); options.setTo(targetEPR); // 命名空间 :targetNamespace = http://server.emplatform.gdt.com.cn ,方法名称:name="queryCompanyRootId" QName opAddEntry = new QName("http://server.emplatform.gdt.com.cn", "queryAbnormalDatas"); // 402880e450db5ad40150db8015fb0000 请求参数 Object[] opAddEntryArgs = new Object[] { "ff808081537288e7015373160f790002","2016-03-25 17:46:37","2016-03-25 19:46:35" }; Class[] classes = new Class[]{String.class}; String str = (String)serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,classes)[0]; System.out.println("调用完毕。。。str==。"+str); } catch (AxisFault e) { e.printStackTrace(); } } }
所需要的jar包
axis2的所有jar包下载地址