cxf_axis_xfire客户端调用的几种方法.docx_第1页
cxf_axis_xfire客户端调用的几种方法.docx_第2页
cxf_axis_xfire客户端调用的几种方法.docx_第3页
cxf_axis_xfire客户端调用的几种方法.docx_第4页
cxf_axis_xfire客户端调用的几种方法.docx_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

CXF、AXIS2、XFire客户端的几种调用方法一、CXF客户端服务接口类WebServicepublic interface TestService public String sayHi(String s);XmlJavaTypeAdapter(StringStringMapAdapter.class)public Map getMap(XmlJavaTypeAdapter(StringStringMapAdapter.class) Map map);public List getList(List list);public User getUser(User user);说明:由于Web服务中不支持直接传Map参数,所以这里我们要写一个XML与Java的类型适配器,实现Java与XML类型的编组与解组。Map类型的适配器类:XmlType(name = StringStringMap)XmlAccessorType(XmlAccessType.FIELD)public class StringStringMap XmlElement(nillable = false, name = entry)List entries=new ArrayList();public List getEntries() return entries; XmlAccessorType(XmlAccessType.FIELD) XmlType(name = IdentifiedString) static class StringStringEntry XmlElement(required = true, nillable = false)String id;String value;public String getId() return id;public void setId(String id) this.id = id;public String getValue() return value;public void setValue(String value) this.value = value;public class StringStringMapAdapter extends XmlAdapterStringStringMap,MapOverridepublic StringStringMap marshal(Map v) throws Exception StringStringMap map=new StringStringMap();for(Map.Entry e:v.entrySet()StringStringMap.StringStringEntry sse=new StringStringMap.StringStringEntry();sse.setId(e.getKey();sse.setValue(e.getValue();map.getEntries().add(sse);return map;Overridepublic Map unmarshal(StringStringMap v) throws Exception Map map = new HashMap();for (StringStringMap.StringStringEntry e : v.getEntries() map.put(e.getId(), e.getValue(); return map;说明:StringStringMapAdapter中marshal()用于将Map转为WebService支持的List(使用StringStringMap包装),unmarshal()用于将List转为Java的Map类型。服务实现类WebService(serviceName=TestService,endpointInterface=com.service.TestService)public class TestServiceImpl implements TestService public String sayHi(String s) return Hello +s;public Map getMap(Map map) return map;public List getList(List list) return list;public User getUser(User user) return user;说明:为了简单起见,我们这里将传过来的参数直接返回给客户端客户端实现类(JaxWsDynamicClientFactory)public class TestServiceClient Test /动态调用public void testSayHi() throws Exception JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();Client client = factory.createClient(http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl);Object res = client.invoke(sayHi, new Object 张三 );System.out.println(Echo response: + res0);/静态调用Testpublic void testSayHi1() throws MalformedURLException QName port_name = new QName(/,TestServiceImplPort);QName service_name = new QName(/, TestService);String url = http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl;Service service = Service.create(service_name);service.addPort(port_name, SOAPBinding.SOAP11HTTP_BINDING, url);TestService test = service.getPort(TestService.class);System.out.println(test.sayHi(张三);严重: The message content list of the in message and out message are same, CXF cant set the holder object into the message content list of the out message.Testpublic void testMap() throws Exception JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();Client client = factory.createClient(http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl);StringStringMap argmap = new StringStringMap();List argentries = new ArrayList();StringStringMap.StringStringEntry argentry1 = new StringStringMap.StringStringEntry();StringStringMap.StringStringEntry argentry2 = new StringStringMap.StringStringEntry();argentry1.setId(1);argentry1.setValue(张三1);argentry2.setId(2);argentry2.setValue(张三2);argentries.add(argentry1);argentries.add(argentry2);argmap.setEntries(argentries);Object res = client.invoke(getMap, new Object argmap );StringStringMap res_map = (StringStringMap) res0;List entries = res_map.getEntries();StringStringMap.StringStringEntry entry = null;Iterator it = entries.iterator();while (it.hasNext() entry = (StringStringMap.StringStringEntry) it.next();System.out.println(key= + entry.getId() + ,value=+ entry.getValue();Testpublic void testList() throws Exception JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();Client client = factory.createClient(http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl);List list = new ArrayList();list.add(张三1);list.add(张三2);Object res = client.invoke(getList, new Object list );System.out.println(Echo response: + res0);Testpublic void testGetUser() throws Exception JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();Client client = factory.createClient(http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl);User user = new User();List list = new ArrayList();Map map = new HashMap();list.add(张三);map.put(1, 张三1);user.setName(张三);user.setPassword(123456);user.setList(list);user.setMap(map);Object res = client.invoke(getUser, new Object user );User res_user = (User) res0;System.out.println(res_user);/通过反射实现服务的调用Testpublic void testGetUserForReflection() throws Exception JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();Client client = factory.createClient(http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl);Object user=Thread.currentThread().getContextClassLoader().loadClass(com.service.User).newInstance();Method setName=user.getClass().getMethod(setName, String.class);Method setPassword=user.getClass().getMethod(setPassword, String.class);setName.invoke(user, 张三);setPassword.invoke(user, 123456);Object res_user = client.invoke(getUser,user)0;Method getName=res_user.getClass().getMethod(getName);Method getPassword=res_user.getClass().getMethod(getPassword);String name=(String)getName.invoke(res_user);String password=(String)getPassword.invoke(res_user);System.out.println(name=+name+,password=+password);Test public void testGetUsers() throws Exception JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();Client client = factory.createClient(http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl);User user1 = new User();User user2 = new User();List list = new ArrayList();Map map = new HashMap();list.add(张三);map.put(1, 张三1);user1.setName(张三1);user1.setPassword(123456);user1.setList(list);user1.setMap(map);user2.setName(张三2);user2.setPassword(123456);user2.setList(list);user2.setMap(map);User users = user1, user2 ;Object res = client.invoke(getUsers, new Object users );User res_users = (User) res0;for (int i = 0; i res_users.length; i+) System.out.println(res_users0);Testpublic void testGetStrs() throws Exception JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();Client client = factory.createClient(http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl); String strs=new String张三1,张三2;Object res = client.invoke(getStrs, new Object strs );System.out.println(res0);说明:在使用集合类型作为参数时最好使用泛型指定元素的类型,不然可能会报错误;在getMap()方法中我们使用的是StringStringMap作为参数而不是Map,服务端会调用StringStringMapAdapter中marshal()将其转为Map,返回Map时是一个反向过程调用unmarshal();所以客户端获得的结果也是StringStringMap。testGetUserForReflection()方法中我们通过Thread.currentThread().getContextClassLoader().loadClass(com.service.User).newInstance()其中Thread.currentThread().getContextClassLoader()获得的是一个URLClassLoader,它的classes中包含了服务端的com.service.User类,所以即使客户端没有相应的User类,我们也可以通过反射调用User中的相应方法设置属性值,最后将其作为参数传给服务端。客户端实现类(wsdl2java生成客户端存根类)通过使用CXF自带的wsdl2java工具生成客户端存根类:由于生成的类太多不一一列出,只说明wsdl2java的最基本的用法:wsdl2java -p com.wsdltojava -d F:开源框架apache-cxf-2.1bin http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl参数说明:-p:指定包名-d:指定生成的代码存放的目录这里我们用JDK1.6自带的wsimport也可以生成同样的代码wsimport -s src -p com.wsdltojava http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl生成的代码如下图import java.util.ArrayList;import java.util.Iterator;import java.util.List;import org.junit.Test;import com.wsdltojava.IdentifiedString;import com.wsdltojava.StringStringMap;import com.wsdltojava.TestService;import com.wsdltojava.TestService_Service;import com.wsdltojava.User;public class WsdlToJavaClient Testpublic void testSayHi() TestService service = new TestService_Service().getTestServiceImplPort();String s = service.sayHi(张三1);System.out.println(s);Testpublic void testMap() StringStringMap map = new StringStringMap();List entries = new ArrayList();IdentifiedString t1 = new IdentifiedString();t1.setId(1);t1.setValue(张三1);IdentifiedString t2 = new IdentifiedString();t2.setId(2);t2.setValue(张三2);entries.add(t1);entries.add(t2);map.setEntries(entries);TestService service = new TestService_Service().getTestServiceImplPort();StringStringMap res_map = service.getMap(map);List res_entries = res_map.getEntries();IdentifiedString entry = null;Iterator it = res_entries.iterator();while (it.hasNext() entry = (IdentifiedString) it.next();System.out.println(key= + entry.getId() + ,value=+ entry.getValue();Testpublic void getUser() User.Map map = new User.Map();List enties = new ArrayList();User.Map.Entry entry1 = new User.Map.Entry();User.Map.Entry entry2 = new User.Map.Entry();entry1.setKey(1);entry1.setValue(张三1);entry2.setKey(2);entry2.setValue(张三2);enties.add(entry1);enties.add(entry2);map.setEntry(enties);List list = new ArrayList();list.add(张三1);list.add(张三2);User user = new User();user.setName(张三);user.setPassword(123456);user.setMap(map);user.setStrs(list);TestService service = new TestService_Service().getTestServiceImplPort();User res_user=service.getUser(user);System.out.println(res_user.toString();客户端实现类(用JAX-WS APIs开发动态客户端)public class DynamicClientForJAXWSAPI Testpublic void testSayHi() throws SOAPExceptionString endpointUrl=http:/localhost:8081/cxf2_4Service/webservices/TestService?wsdl;QName serviceName=new QName(/, TestService);QName portName=new QName(/, TestServiceImplPort);/* 创建一个service并且至少要添加一个port */Service service=Service.create(serviceName);service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointUrl);/* 从service创建dispatch实例 */Dispatch dispatch=service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);/* 创建SOAPMessage请求 */MessageFactory mf=MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);SOAPMessage request=mf.createMessage();SOAPPart part=request.getSOAPPart();/* SOAPEnvelope、SOAPHeader、SOAPBody */SOAPEnvelope env=part.getEnvelope();SOAPHeader header=env.getHeader();SOAPBody body=env.getBody();/* 构造消息的有效载荷 */SOAPElement operation=body.addChildElement(sayHi, ns1, /);SOAPElement value=operation.addChildElement(arg0);value.addTextNode(张三);request.saveChanges();/* 调用服务 */SOAPMessage response=dispatch.invoke(request);/* 获取返回的结果 */String res=response.getSOAPBody().getElementsByTagName(return).item(0).getTextContent();System.out.println(res);说明:dispatch客户端是一个面向XML消息的客户端,这里我们只是用JAX-WS APIs实现了一个简单的客户端调用,服务端返回的是一个字符串,如果返回的是一个JavaBean那么我们需要通过返回的SOAP包自己去构造一个Javabean。二、AXIS2客户端这里我们不写AXIS2的服务端,直接使用上面的CXF的服务端。客户端实现类(RPCServiceClient)public class TestServiceAxis2Client Testpublic void testSayHi() throws AxisFaultRPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();/ 指定调用WebService的URLEndpointReference targetEPR = new EndpointReference(http:/localhost:8080/cxf2_4Service/webservices/TestService);options.setTo(targetEPR);/ 指定sayHi方法的参数值Object opAddEntryArgs = new Object 张三 ;/ 指定sayHi方法返回值的数据类型的Class对象Class classes = new Class String.class ;/ 指定要调用的sayHi方法及WSDL文件的命名空间QName opAddEntry = new QName(/,sayHi);/ 调用sayHi方法并输出该方法的返回值Object res=serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)0;System.out.println(res);Testpublic void testMap() throws AxisFaultRPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();EndpointReference targetEPR = new EndpointReference(http:/localhost:8081/cxf2_4Service/webservices/TestService);options.setTo(targetEPR);StringStringMap argmap = new StringStringMap();List argentries = new ArrayList();StringStringMap.StringStringEntry argentry1 = new StringStringMap.StringStringEntry();StringStringMap.StringStringEntry argentry2 = new StringStringMap.StringStringEntry();argentry1.setId(1);argentry1.setValue(张三1);argentry2.setId(2);argentry2.setValue(张三2);argentries.add(argentry1);argentries.add(argentry2);argmap.setEntries(argentries);Object opAddEntryArgs = new Object argmap ;Class classes = new Class StringStringMap.class ;QName opAddEntry = new QName(/,getMap);Object res=serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes);StringStringMap res_map = (StringStringMap) res0;List entries = res_map.getEntries();OMElementImpl entry = null;Iterator it = entries.iterator();while (it.hasNext() entry = (OMElementImpl) it.next();Iterator e=entry.getChildren();while(e.hasNext()OMElementImpl ele=e.next();System.out.print(ele.getText();System.out.println();Test/有问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题问题public void testList() throws AxisFault/有问题RPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();EndpointReference targetEPR = new EndpointReference(http:/localhost:8081/cxf2_4Service/webservices/TestService);options.setTo(targetEPR);List list = new ArrayList();list.add(张三1);list.add(张三2);Object opAddEntryArgs = new Object list ;Class classes = new Class List.class ;QName opAddEntry = new QName(/,getList);Object res=serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)0;System.out.println(res);说明:testMap()方法中返回结果的StringStringMap中的List的元素并不是CXF中的StringStringEntry而是OMElementImpl类似与读取XML文档元素的类(2张三2, 1张三1)通过调用getText()方法获得其值。客户端实现类(用AXIS2 AXIOMAXis Object Model)import java.rmi.RemoteException;import org.apache.axiom.om.OMAbstractFactory;import org.apache.axiom.om.OMElement;import org.apache.axiom.om.OMFactory;import org.apache.axiom.om.OMNamespace;import org.apache.axis2.Constants;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.client.ServiceClient;import org.junit.Test;public class AXIOMClient Testpublic void testSayHi() throws RemoteException EndpointReference targetEPR = new EndpointReference(http:/localhost:8081/cxf2_4Service/webservices/TestService);OMFactory fac=OMAbstractFactory.getOMFactory();OMNamespace omNs = fac.createOMNamespace(/, ns1);OMElement method = fac.createOMElement(sayHi, omNs);OMElement value = fac.createOMElement(arg0,fac.createOMNamespace(, );value.addChild(fac.createOMText(value,张三);method.addChild(value);Options options = new Options();options.setTo(targetEPR);options.setAction(/);options.setTransportInProtocol(Constants.TRANSPORT_HTTP);ServiceClient client = new ServiceClient();client.setOptions(options);OMElement result = client.sendReceive(method);String response = result.getFirstElement().getText();System.out.println(response);客户端实现类(AXIS2 ADBAxis Data Binding)通过Axis2自带的wsdl2java工具生成客户端桩类;最基本的用法:wsdl2java -uri http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl -p com.wsdltojava -o stubAxi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论