版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、CXF、AXIS2、XFire客戶端的幾種調(diào)用方法一、CXF客戶端服務(wù)接口類(lèi)WebServicepublic interface TestService public String sayHi(String s);XmlJavaTypeAdapter(StringStringMapAdapter.class)public Map<String,String> getMap(XmlJavaTypeAdapter(StringStringMapAdapter.class) Map<String,String> map);public List getList(List<
2、;String> list);public User getUser(User user);說(shuō)明:由于Web服務(wù)中不支持直接傳Map參數(shù),所以這里我們要寫(xiě)一個(gè)XML與Java的類(lèi)型適配器,實(shí)現(xiàn)Java與XML類(lèi)型的編組與解組。Map類(lèi)型的適配器類(lèi):XmlType(name = "StringStringMap")XmlAccessorType(XmlAccessType.FIELD)public class StringStringMap XmlElement(nillable = false, name = "entry")List<Str
3、ingStringEntry> entries=new ArrayList<StringStringEntry>();public List<StringStringEntry> getEntries() return entries; XmlAccessorType(XmlAccessType.FIELD) XmlType(name = "IdentifiedString") static class StringStringEntry XmlElement(required = true, nillable = false)String i
4、d;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 XmlAdapter<StringStringMap,Map<String,String>>Overridepublic St
5、ringStringMap marshal(Map<String, String> v) throws Exception StringStringMap map=new StringStringMap();for(Map.Entry<String, String> e:v.entrySet()StringStringMap.StringStringEntry sse=new StringStringMap.StringStringEntry();sse.setId(e.getKey();sse.setValue(e.getValue();map.getEntries(
6、).add(sse);return map;Overridepublic Map<String, String> unmarshal(StringStringMap v) throws Exception Map<String, String> map = new HashMap<String, String>();for (StringStringMap.StringStringEntry e : v.getEntries() map.put(e.getId(), e.getValue(); return map;說(shuō)明:StringStringMapAda
7、pter中marshal()用于將Map轉(zhuǎn)為WebService支持的List(使用StringStringMap包裝),unmarshal()用于將List轉(zhuǎn)為Java的Map類(lèi)型。服務(wù)實(shí)現(xiàn)類(lèi)WebService(serviceName="TestService",endpointInterface="com.service.TestService")public class TestServiceImpl implements TestService public String sayHi(String s) return "Hello &
8、quot;+s;public Map<String, String> getMap(Map<String,String> map) return map;public List getList(List<String> list) return list;public User getUser(User user) return user;說(shuō)明:為了簡(jiǎn)單起見(jiàn),我們這里將傳過(guò)來(lái)的參數(shù)直接返回給客戶端客戶端實(shí)現(xiàn)類(lèi)(JaxWsDynamicClientFactory)public class TestServiceClient Test /動(dòng)態(tài)調(diào)用public v
9、oid 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 "張三" );Sys
10、tem.out.println("Echo response: " + res0);/靜態(tài)調(diào)用Testpublic void testSayHi1() throws MalformedURLException QName port_name = new QName(""TestServiceImplPort");QName service_name = new QName(" "TestService");String url = "http:/localhost:8080/cxf2_4Service/w
11、ebservices/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("張三");嚴(yán)重: The message content list of the in message and out m
12、essage are same, CXF can't 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/w
13、ebservices/TestService?wsdl");StringStringMap argmap = new StringStringMap();List<StringStringMap.StringStringEntry> argentries = new ArrayList<StringStringEntry>();StringStringMap.StringStringEntry argentry1 = new StringStringMap.StringStringEntry();StringStringMap.StringStringEntr
14、y 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(&qu
15、ot;getMap", new Object argmap );StringStringMap res_map = (StringStringMap) res0;List<StringStringMap.StringStringEntry> entries = res_map.getEntries();StringStringMap.StringStringEntry entry = null;Iterator it = entries.iterator();while (it.hasNext() entry = (StringStringMap.StringString
16、Entry) 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_4S
17、ervice/webservices/TestService?wsdl");List<String> list = new ArrayList<String>();list.add("張三1");list.add("張三2");Object res = client.invoke("getList", new Object list );System.out.println("Echo response: " + res0);Testpublic void testGetUser(
18、) throws Exception JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();Client client = factory.createClient("http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl");User user = new User();List<String> list = new ArrayList<String>();Map<Strin
19、g, String> map = new HashMap<String, String>();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_us
20、er = (User) res0;System.out.println(res_user);/通過(guò)反射實(shí)現(xiàn)服務(wù)的調(diào)用Testpublic void testGetUserForReflection() throws Exception JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();Client client = factory.createClient("http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl
21、");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
22、, "張三");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
23、_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(&qu
24、ot;http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl");User user1 = new User();User user2 = new User();List<String> list = new ArrayList<String>();Map<String, String> map = new HashMap<String, String>();list.add("張三");map.put("1", "
25、張三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",
26、 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:/loc
27、alhost:8080/cxf2_4Service/webservices/TestService?wsdl"); String strs=new String"張三1","張三2"Object res = client.invoke("getStrs", new Object strs );System.out.println(res0);說(shuō)明:在使用集合類(lèi)型作為參數(shù)時(shí)最好使用泛型指定元素的類(lèi)型,不然可能會(huì)報(bào)錯(cuò)誤;在getMap()方法中我們使用的是StringStringMap作為參數(shù)而不是Map,服務(wù)端會(huì)調(diào)用Strin
28、gStringMapAdapter中marshal()將其轉(zhuǎn)為Map,返回Map時(shí)是一個(gè)反向過(guò)程調(diào)用unmarshal();所以客戶端獲得的結(jié)果也是StringStringMap。testGetUserForReflection()方法中我們通過(guò)Thread.currentThread().getContextClassLoader().loadClass("com.service.User").newInstance()其中Thread.currentThread().getContextClassLoader()獲得的是一個(gè)URLClassLoader,它的classe
29、s中包含了服務(wù)端的com.service.User類(lèi),所以即使客戶端沒(méi)有相應(yīng)的User類(lèi),我們也可以通過(guò)反射調(diào)用User中的相應(yīng)方法設(shè)置屬性值,最后將其作為參數(shù)傳給服務(wù)端??蛻舳藢?shí)現(xiàn)類(lèi)(wsdl2java生成客戶端存根類(lèi))通過(guò)使用CXF自帶的wsdl2java工具生成客戶端存根類(lèi):由于生成的類(lèi)太多不一一列出,只說(shuō)明wsdl2java的最基本的用法:wsdl2java -p com.wsdltojava -d F:開(kāi)源框架apache-cxf-2.1bin http:/localhost:8080/cxf2_4Service/webservices/TestService?wsdl參數(shù)說(shuō)明:-p
30、:指定包名-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
31、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&
32、quot;);System.out.println(s);Testpublic void testMap() StringStringMap map = new StringStringMap();List<IdentifiedString> entries = new ArrayList<IdentifiedString>();IdentifiedString t1 = new IdentifiedString();t1.setId("1");t1.setValue("張三1");IdentifiedString t2 = ne
33、w 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<IdentifiedString> res_entries = res_map.
34、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<User.Ma
35、p.Entry> enties = new ArrayList<User.Map.Entry>();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.a
36、dd(entry2);map.setEntry(enties);List<String> list = new ArrayList<String>();list.add("張三1");list.add("張三2");User user = new User();user.setName("張三");user.setPassword("123456");user.setMap(map);user.setStrs(list);TestService service = new TestServi
37、ce_Service().getTestServiceImplPort();User res_user=service.getUser(user);System.out.println(res_user.toString();客戶端實(shí)現(xiàn)類(lèi)(用JAX-WS APIs開(kāi)發(fā)動(dòng)態(tài)客戶端)public class DynamicClientForJAXWSAPI Testpublic void testSayHi() throws SOAPExceptionString endpointUrl="http:/localhost:8081/cxf2_4Service/webservices/Te
38、stService?wsdl"QName serviceName=new QName(" "TestService");QName portName=new QName(" "TestServiceImplPort");/* 創(chuàng)建一個(gè)service并且至少要添加一個(gè)port */Service service=Service.create(serviceName);service.addPort(portName, SOAPBinding.SOAP11HTTP_BINDING, endpointUrl);/* 從servic
39、e創(chuàng)建dispatch實(shí)例 */Dispatch<SOAPMessage> dispatch=service.createDispatch(portName, SOAPMessage.class, Service.Mode.MESSAGE);/* 創(chuàng)建SOAPMessage請(qǐng)求 */MessageFactory mf=MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);SOAPMessage request=mf.createMessage();SOAPPart part=request.getSOAPPart()
40、;/* SOAPEnvelope、SOAPHeader、SOAPBody */SOAPEnvelope env=part.getEnvelope();SOAPHeader header=env.getHeader();SOAPBody body=env.getBody();/* 構(gòu)造消息的有效載荷 */SOAPElement operation=body.addChildElement("sayHi", "ns1", "SOAPElement value=operation.addChildElement("arg0");v
41、alue.addTextNode("張三");request.saveChanges();/* 調(diào)用服務(wù) */SOAPMessage response=dispatch.invoke(request);/* 獲取返回的結(jié)果 */String res=response.getSOAPBody().getElementsByTagName("return").item(0).getTextContent();System.out.println(res);說(shuō)明:dispatch客戶端是一個(gè)面向XML消息的客戶端,這里我們只是用JAX-WS APIs實(shí)現(xiàn)了一個(gè)
42、簡(jiǎn)單的客戶端調(diào)用,服務(wù)端返回的是一個(gè)字符串,如果返回的是一個(gè)JavaBean那么我們需要通過(guò)返回的SOAP包自己去構(gòu)造一個(gè)Javabean。二、AXIS2客戶端這里我們不寫(xiě)AXIS2的服務(wù)端,直接使用上面的CXF的服務(wù)端??蛻舳藢?shí)現(xiàn)類(lèi)(RPCServiceClient)public class TestServiceAxis2Client Testpublic void testSayHi() throws AxisFaultRPCServiceClient serviceClient = new RPCServiceClient();Options options = serviceClie
43、nt.getOptions();/ 指定調(diào)用WebService的URLEndpointReference targetEPR = new EndpointReference("http:/localhost:8080/cxf2_4Service/webservices/TestService");options.setTo(targetEPR);/ 指定sayHi方法的參數(shù)值Object opAddEntryArgs = new Object "張三" ;/ 指定sayHi方法返回值的數(shù)據(jù)類(lèi)型的Class對(duì)象Class classes = new Cl
44、ass String.class ;/ 指定要調(diào)用的sayHi方法及WSDL文件的命名空間QName opAddEntry = new QName(""sayHi");/ 調(diào)用sayHi方法并輸出該方法的返回值Object res=serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)0;System.out.println(res);Testpublic void testMap() throws AxisFaultRPCServiceClient serviceClient = new
45、RPCServiceClient();Options options = serviceClient.getOptions();EndpointReference targetEPR = new EndpointReference("http:/localhost:8081/cxf2_4Service/webservices/TestService");options.setTo(targetEPR);StringStringMap argmap = new StringStringMap();List<StringStringMap.StringStringEntr
46、y> argentries = new ArrayList<StringStringEntry>();StringStringMap.StringStringEntry argentry1 = new StringStringMap.StringStringEntry();StringStringMap.StringStringEntry argentry2 = new StringStringMap.StringStringEntry();argentry1.setId("1");argentry1.setValue("張三1");a
47、rgentry2.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");Objec
48、t res=serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes);StringStringMap res_map = (StringStringMap) res0;List<StringStringMap.StringStringEntry> entries = res_map.getEntries();OMElementImpl entry = null;Iterator it = entries.iterator();while (it.hasNext() entry = (OMElementImpl)
49、 it.next();Iterator<OMElementImpl> e=entry.getChildren();while(e.hasNext()OMElementImpl ele=e.next();System.out.print(ele.getText();System.out.println();Test/有問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題問(wèn)題public void testList() throws AxisFault/有問(wèn)題RPCServiceClient serviceClient = new RPCServiceClient();Opti
50、ons options = serviceClient.getOptions();EndpointReference targetEPR = new EndpointReference("http:/localhost:8081/cxf2_4Service/webservices/TestService");options.setTo(targetEPR);List<String> list = new ArrayList<String>();list.add("張三1");list.add("張三2");Ob
51、ject 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);說(shuō)明:testMap()方法中返回結(jié)果的StringStringMap中的List的元素并不是CXF中的StringStringEn
52、try而是OMElementImpl類(lèi)似與讀取XML文檔元素的類(lèi)(<entries><id>2</id><value>張三2</value></entries>, <entries><id>1</id><value>張三1</value></entries>)通過(guò)調(diào)用getText()方法獲得其值??蛻舳藢?shí)現(xiàn)類(lèi)(用AXIS2 AXIOMAXis Object Model)import java.rmi.RemoteException;import or
53、g.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.c
54、lient.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
55、 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);
56、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);客戶端實(shí)現(xiàn)類(lèi)(AXIS2 ADBA
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度航空器改裝及性能提升合同3篇
- 二零二五年度臨時(shí)工酒店餐飲服務(wù)合同4篇
- 全球電子商務(wù)稅收政策2025年度協(xié)調(diào)執(zhí)行合同
- 2025年度毛皮原料電商平臺(tái)開(kāi)發(fā)合同4篇
- 食品加工行業(yè)二零二五年度食品安全HSE協(xié)議范本3篇
- 2025-2030年中國(guó)金屬銦行業(yè)發(fā)展?fàn)顩r及投資前景研究報(bào)告
- 2025-2030年中國(guó)造紙級(jí)滑石粉行業(yè)發(fā)展趨勢(shì)及投資策略分析報(bào)告
- 2025-2030年中國(guó)過(guò)硫酸鹽行業(yè)運(yùn)行現(xiàn)狀及發(fā)展前景預(yù)測(cè)報(bào)告
- 2025-2030年中國(guó)輕質(zhì)建筑材料制造市場(chǎng)發(fā)展?fàn)顩r及營(yíng)銷(xiāo)戰(zhàn)略研究報(bào)告
- 2025-2030年中國(guó)血凝儀產(chǎn)業(yè)市場(chǎng)發(fā)展?fàn)顩r及投資前景規(guī)劃研究報(bào)告
- 2024年度醫(yī)院肝膽外科實(shí)習(xí)生帶教計(jì)劃課件
- 微機(jī)原理與接口技術(shù)考試試題及答案(綜合-必看)
- 勞務(wù)投標(biāo)技術(shù)標(biāo)
- 研發(fā)管理咨詢(xún)項(xiàng)目建議書(shū)
- 濕瘡的中醫(yī)護(hù)理常規(guī)課件
- 轉(zhuǎn)錢(qián)委托書(shū)授權(quán)書(shū)范本
- 一種配網(wǎng)高空作業(yè)智能安全帶及預(yù)警系統(tǒng)的制作方法
- 某墓園物業(yè)管理日常管護(hù)投標(biāo)方案
- 蘇教版六年級(jí)數(shù)學(xué)上冊(cè)集體備課記載表
- 內(nèi)蒙古匯能煤電集團(tuán)有限公司長(zhǎng)灘露天煤礦礦山地質(zhì)環(huán)境保護(hù)與土地復(fù)墾方案
- 22S702 室外排水設(shè)施設(shè)計(jì)與施工-鋼筋混凝土化糞池
評(píng)論
0/150
提交評(píng)論