困扰很久的ROR调用Axis2 web service得到初步解决
问题:ROR如何调用Axis2的web service
目前遇到这个问题,在Google和百度搜了无数次,国内国外的都看过,几乎没有任何有价值的资料,唯一有所帮助的是一篇关于ROR调用.Net服务的文章(http://qiezi.iteye.com/blog/26642)。仿照该文章的方法,做如下尝试:
java写的service
package sample;import org.apache.axiom.om.OMAbstractFactory;import org.apache.axiom.om.OMElement;import org.apache.axiom.om.OMFactory;import org.apache.axiom.om.OMNamespace;public class HelloWorld { public OMElement SayHello(OMElement in){ String name=in.getText(); String info=name+"HelloWorld!"; OMFactory fac=OMAbstractFactory.getOMFactory(); OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw"); OMElement resp=fac.createOMElement("SayHelloResponse",omNs); resp.setText(info); return resp; }}class TestApi < ActionWebService::API::Base api_method :SayHello, :expects =>[:string], :returns=>[:string]end
class TestController < ApplicationController web_client_api :test, :soap, "http://172.23.77.12:8080/axis2/services/HelloWorld", :namespace => "http://helloworld.com/", :soap_action_base => "http://172.23.77.12:8080/axis2/services/HelloWorld", :driver_options => {:default_encodingstyle => SOAP::EncodingStyle::SOAPHandler::Namespace } def hello render_text test.SayHello("You!") endendpackage example.client;//Guess what i will do?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.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.client.ServiceClient;public class TestClient { private static EndpointReference targetEPR=new EndpointReference ("http://172.23.77.12:8080/axis2/services/HelloWorld"); public static OMElement getSayHelloOMElement(){ OMFactory fac=OMAbstractFactory.getOMFactory(); OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw"); OMElement method=fac.createOMElement("SayHello",omNs); method.setText("You!"); return method; } public static void main(String[] args){ try{ Options options=new Options(); options.setTo(targetEPR); ServiceClient sender=new ServiceClient(); sender.setOptions(options); OMElement sayHello=TestClient.getSayHelloOMElement(); OMElement result=sender.sendReceive(SayHello); System.out.println(result); } catch(Exception axisFault){ axisFault.printStackTrace(); } }}<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Header /><soapenv:Body><hw:sayHello xmlns:hw="http://helloworld.com/">You!</hw:sayHello></soapenv:Body></soapenv:Envelope>
<?xml version="1.0" encoding="utf-8" ?><env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <env:Body> <n1:SayHello xmlns:n1="http://helloworld.com/" env:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> <param0 xsi:type="xsd:string">You!</param0> </n1:SayHello> </env:Body></env:Envelope>
OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");conn_data.send_contenttype = mime.headers['content-type'].str end conn_data.send_string.gsub!(/:n1/,':hw') conn_data.send_string.gsub!(/n1:/,'hw:') conn_data.send_string.gsub!(/<param0 xsi:type="xsd:string">/,'') conn_data.send_string.gsub!(/<\/param0>/,'') conn_data.send_string.gsub!(/\n/,'')
<hw:sayHello xmlns:hw="http://helloworld.com/">You!</hw:sayHello>