一天之中,用了各种方式生成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()); } }
生成Webservice的两种方式(Axis2,CXf2.x),布布扣,bubuko.com
生成Webservice的两种方式(Axis2,CXf2.x)
原文:http://www.cnblogs.com/juepei/p/3867672.html