1、建立web 工程,)引入axis相关jar包,编写webservice服务器端(这里为DataTransService.java)
DataTransService.java
package com.trs.service; import net.sf.json.JSONObject; import com.trs.entity.CPDetailResult; import com.trs.entity.CPListResult; import com.trs.entity.CPQuery; import com.trs.entity.ExplainData; public class DataTransService{ public String Explain() { System.out.println("method Explain() excuted"); ExplainData explainData = new ExplainData(); /**测试部分开始...*/ explainData.setName("TRSSERVER数据库"); /**测试部分结束...*/ System.out.println("######## "+explainData.getName()); /** * 这里完成检索代码 */ String jSonString = objectToJsonString(explainData); return jSonString; } public CPListResult GetList(CPQuery q) { System.out.println("method GetList() excuted"); CPListResult listResult = new CPListResult(); System.out.println("CPQuery DBName: "+q.getDBName()); listResult.setMessage(q.getDBName()); /** * 这里完成检索代码 */ String jSonString = objectToJsonString(listResult); return listResult; } public String GetDetail(long detailid){ System.out.println("method GetDetail() excuted"); CPDetailResult detailResult = new CPDetailResult(); /** * 这里完成检索代码 */ String jSonString = objectToJsonString(detailResult); return jSonString; } private String objectToJsonString(Object o) { //对象转json JSONObject jObject = JSONObject.fromObject(o); return jObject.toString(); } }
2、在工程目录zb_service\WebRoot\WEB-INF目录下新建一个deploy.wsdd文件,文件内容为:
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <service name="DataTransService" provider="java:RPC"> <parameter name="className" value="com.trs.service.DataTransService"/> <parameter name="allowedMethods" value="*"/> </service> </deployment>
3、在E:\workspace\zb_service\WebRoot\WEB-INF目录下运行:
java -Djava.ext.dirs=E:\workspace\zb_service\WebRoot\WEB-INF\lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/zb_service/services/AdminService deploy.wsdd
则在zb_service\WebRoot\WEB-INF目录下会生成一个文件server-config.wsdd,文件内容为:
<?xml version="1.0" encoding="UTF-8"?> <deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration> <parameter name="sendMultiRefs" value="true"/> <parameter name="disablePrettyXML" value="true"/> <parameter name="adminPassword" value="admin"/> <parameter name="attachments.Directory" value="D:\Tomcat6.0\webapps\zb_service\WEB-INF\attachments"/> <parameter name="dotNetSoapEncFix" value="true"/> <parameter name="enableNamespacePrefixOptimization" value="false"/> <parameter name="sendXMLDeclaration" value="true"/> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/> <parameter name="sendXsiTypes" value="true"/> <requestFlow> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="session"/> </handler> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="request"/> <parameter name="extension" value=".jwr"/> </handler> </requestFlow> </globalConfiguration> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/> <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/> <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/> <service name="AdminService" provider="java:MSG"> <parameter name="allowedMethods" value="AdminService"/> <parameter name="enableRemoteAdmin" value="false"/> <parameter name="className" value="org.apache.axis.utils.Admin"/> <namespace>http://xml.apache.org/axis/wsdd/</namespace> </service> <service name="DataTransService" provider="java:RPC"> <parameter name="allowedMethods" value="*"/> <parameter name="className" value="com.trs.service.DataTransService"/> <!-- 这里定义了方法的参数以及返回值 --> <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" qname="ns:CPQuery" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.trs.entity.CPQuery" xmlns:ns="urn:MyService"/> <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" qname="ns:CPListResult" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.trs.entity.CPListResult" xmlns:ns="urn:MyService"/> <typeMapping deserializer="org.apache.axis.encoding.ser.BeanDeserializerFactory" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" qname="ns:CPRecord" serializer="org.apache.axis.encoding.ser.BeanSerializerFactory" type="java:com.trs.entity.CPRecord" xmlns:ns="urn:MyService"/> </service> <service name="Version" provider="java:RPC"> <parameter name="allowedMethods" value="getVersion"/> <parameter name="className" value="org.apache.axis.Version"/> </service> <transport name="http"> <requestFlow> <handler type="URLMapper"/> <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/> </requestFlow> <parameter name="qs:list" value="org.apache.axis.transport.http.QSListHandler"/> <parameter name="qs:wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/> <parameter name="qs.list" value="org.apache.axis.transport.http.QSListHandler"/> <parameter name="qs.method" value="org.apache.axis.transport.http.QSMethodHandler"/> <parameter name="qs:method" value="org.apache.axis.transport.http.QSMethodHandler"/> <parameter name="qs.wsdl" value="org.apache.axis.transport.http.QSWSDLHandler"/> </transport> <transport name="local"> <responseFlow> <handler type="LocalResponder"/> </responseFlow> </transport> </deployment>
黑体部分为后来加入的设置
客户端代码:
package com.trs.client; import javax.xml.namespace.QName; import net.sf.json.JSONObject; import org.apache.axis.Constants; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.ser.BeanDeserializerFactory; import org.apache.axis.encoding.ser.BeanSerializerFactory; import com.trs.entity.*; public class DataTransClient { //创建访问点 private static String ENDPOINT = "http://localhost:8080/zb_service/services/DataTransService"; //获取数据库相关说明服务名 private static String DATAEXPLAIN = "Explain"; //获取数据列表服务名 private static String GETLIST = "GetList"; //获取数据细缆服务名 private static String GETDETAIL = "GetDetail"; public void ExplainData(){ try{ //创建service Service service = new Service(); Call call = null; //创建call call = (Call) service.createCall(); //设置操作名 call.setOperationName(new QName(ENDPOINT,DATAEXPLAIN)); //设置访问点 call.setTargetEndpointAddress(new java.net.URL(ENDPOINT)); //调用服务,返回值 String ret = (String) call.invoke(new Object[]{}); JSONObject jsObject = JSONObject.fromObject(ret); System.out.println(("name:" + jsObject.getString("name"))); }catch (Exception ex){ ex.printStackTrace(); } } public void getList(CPQuery query){ try{ //创建service Service service = new Service(); Call call = null; //创建call call = (Call) service.createCall(); QName qName1 = new QName("urn:MyService","CPQuery"); QName qName2 = new QName("urn:MyService","CPListResult"); QName qName3 = new QName("urn:MyService","CPRecord"); //设置操作名 call.setOperationName(new QName(ENDPOINT,GETLIST)); //设置访问点 call.setTargetEndpointAddress(new java.net.URL(ENDPOINT)); // 注册序列化和反序列化类 call.registerTypeMapping(CPQuery.class, qName1, new BeanSerializerFactory(CPQuery.class, qName1), new BeanDeserializerFactory(CPQuery.class, qName1)); call.registerTypeMapping(CPListResult.class, qName2, new BeanSerializerFactory(CPListResult.class, qName2), new BeanDeserializerFactory(CPListResult.class, qName2)); call.registerTypeMapping(CPRecord.class, qName3, new BeanSerializerFactory(CPRecord.class, qName3), new BeanDeserializerFactory(CPRecord.class, qName3)); //调用服务,返回值 CPListResult ret = (CPListResult) call.invoke(new Object[]{query}); System.out.println("返回的信息是:" + ret.getMessage()+ret.getRecords()[0].getDetailID()); }catch (Exception ex){ ex.printStackTrace(); } } public void getDetail(long id){ try{ //创建service Service service = new Service(); Call call = null; QName QNAME = new QName(ENDPOINT,GETDETAIL); //创建call call = (Call) service.createCall(); //设置操作名 call.setOperationName(QNAME); //设置访问点 call.setTargetEndpointAddress(new java.net.URL(ENDPOINT)); //调用服务,返回值 CPDetailResult ret = (CPDetailResult) call.invoke(new Object[]{id}); System.out.println("返回的信息是:" + ret); }catch (Exception ex){ ex.printStackTrace(); } } public static void main(String args[]){ DataTransClient client = new DataTransClient(); // client.ExplainData(); CPQuery query = new CPQuery(); query.setDBName("oracle"); client.getList(query); // long id = 100; // client.getDetail(id); } }
小结:当自定义类型参数或返回值里含有多个自定义类型对象时,对这些包含的自定义类型对象也如做如上的配置,这样看来,使用比较繁琐。Jeson类给我们提供了一个新的方式,在上面的服务器端代码里,jeson对自定义类型的参数和返回值做了字符串的转换,因此,传递自定义类型变成了传递字符串,我们只需在客户端和服务器端代码里对字符串进行jeson转换就可以了。如:
JSONObject jsObject = JSONObject.fromObject(ret); System.out.println(("name:" + jsObject.getString("name")));