调用axis2 WebService三种方法(转载)
第一:简单的使用axis2包自己实现调用
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 WebServiceUtils { // ----------------------------------------------------- Properties public static int TIMEOUT = 100000; // ----------------------------------------------------- Constructors // ----------------------------------------------------- Methods /** * * 调用axis2的webservice的方法 * * @return */ @SuppressWarnings("unchecked") static public Object[] invokeWebService( String url, String nameSpace, String method, Object[] args, Class[] returnTypes ) throws AxisFault { RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference( url ); options.setTo( targetEPR ); options.setTimeOutInMilliSeconds( TIMEOUT ); QName opName = new QName( nameSpace, method ); Object[] results = serviceClient.invokeBlocking( opName, args, returnTypes ); return results; } }
第二、
public static OMElement getEchoOMElement() { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace( "http://example1.org/example1", "example1"); OMElement method = fac.createOMElement("echo", omNs); OMElement value = fac.createOMElement("Text", omNs); value.addChild(fac.createOMText(value, "Axis2 Echo String ")); method.addChild(value); return method; } public static void main(String[] args) { try { OMElement payload = ClientUtil.getEchoOMElement(); Options options = new Options(); options.setTo(targetEPR); options.setAction("urn:echo"); //Blocking invocation ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMElement result = sender.sendReceive(payload); System.out.println(result); System.out.println(options.getTo()); } catch (AxisFault axisFault) { axisFault.printStackTrace(); } }
第三、利用插件生成客户端
private static String URL="http://localhost:8080/axis2/services/WeatherService"; public static void main(String[] args) throws RemoteException { try { WeatherServiceStub stub; if (args != null && args.length != 0) { stub = new WeatherServiceStub(args[0]); } else { stub = new WeatherServiceStub(URL); } WeatherServiceStub.SetWeather setWether = new WeatherServiceStub.SetWeather(); WeatherServiceStub.Weather wether = new WeatherServiceStub.Weather(); wether.setForecast("east"); wether.setRain(true); wether.setTemperature(12.3f); wether.setForecast("big"); setWether.setWeather(wether); stub.setWeather(setWether); WeatherServiceStub.GetWeatherResponse gw = new WeatherServiceStub.GetWeatherResponse(); System.out.println(gw.get_return()); } catch (AxisFault e) { // TODO Auto-generated catch block e.printStackTrace(); } }

浙公网安备 33010602011771号