一天之中,用了各種方式生成webservice,就是為了node.js能和程序順利通信。最終還是用axis2成功了。工作基本完成了,現在可以總結一下。
關於生成方式,推薦使用eclipse,比較方便,具體方式,上篇博文有轉載,axis2和cxf2.x都是如此。注意:不是在項目上new一個webservice,是在你要作為webservice調用類的上面new一個。
具體代碼,和測試代碼,均通過測試的。
一、Axis2
服務端:
package cn.wang; public class EventForwarder { public String send(String message){ return "hello word"+message; } }
客戶端調用send方法的時候,這里就會返回給他們一個我們拼的字符串。
客戶端:
package cn.test; 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 Client { public static void main(String[] args) throws AxisFault { RPCServiceClient serviceClient=new RPCServiceClient(); Options options=serviceClient.getOptions(); EndpointReference targetEPR=new EndpointReference("http://localhost:8080/WebService/services/HelloWorld"); //這個寫自己生成的webservice地址 options.setTo(targetEPR); Object[] send=new Object[]{"zzz"};//the type of send Class[] recevied=new Class[]{String.class}; //the result type of receved QName qName=new QName("http://wang.cn","send"); String result=serviceClient.invokeBlocking(qName, send, recevied)[0]; System.out.println(result); } }
二、Cxf 2.x
這種比較接近標准。
服務端:
接口:
package com.gnivcode.spms.process.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; @WebService(targetNamespace="http://service.process.spms.gnivcode.com/") public interface EventForwarderInterface { @WebMethod(operationName = "send") @WebResult(name = "feedResult") public FeedResult send(@WebParam(name="nocPacket",targetNamespace="http://service.process.spms.gnivcode.com/")NocPacket nocPacket); @WebMethod(operationName = "process") @WebResult(name = "feedResult") public FeedResult process(@WebParam(name="eventPacket",targetNamespace="http://service.process.spms.gnivcode.com/")EventPacket eventPacket); }
package com.gnivcode.spms.process.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService(targetNamespace = "http://service.process.spms.gnivcode.com/", portName = "EventForwarderPort", serviceName = "EventForwarderService") public class EventForwarder implements EventForwarderInterface{ public FeedResult process(@WebParam(name = "eventPacket") EventPacket eventPacket){ FeedResult feedResult=new FeedResult(); feedResult.setResultDesc("hi,feedresult"); return feedResult; } public FeedResult send(@WebParam(name = "nocPacket") NocPacket nocPacket){ FeedResult feedResult=new FeedResult(); feedResult.setResultDesc("hi,feedresult"); return feedResult; } }
不使用接口也可以,和上面的一樣,具體也忘記了。
客戶端:
package cn.wang.test; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.gnivcode.spms.process.service.EventForwarderInterface; import com.gnivcode.spms.process.service.FeedResult; import com.gnivcode.spms.process.service.NocPacket; public class Client { public static void main(String[] args) { // TODO Auto-generated method stub JaxWsProxyFactoryBean factory=new JaxWsProxyFactoryBean(); factory.setServiceClass(EventForwarderInterface.class); factory.setAddress("http://localhost:8080/MessageProcess/services/EventForwarderPort"); EventForwarderInterface eventForwarderInterface=(EventForwarderInterface) factory.create(); NocPacket nocPacket=new NocPacket(); nocPacket.setGWName("noc_gwname"); FeedResult feedResult=eventForwarderInterface.send(nocPacket); System.out.println("feedresult desc:"+feedResult.getResultDesc()); System.out.println("feedresult code:"+feedResult.getResultCode()); } }