您的位置:首页 > 其它

整理了一点REST的资料和例子

2007-08-27 09:56 471 查看
理解一个新的概念其实不是件容易事,特别是对于语言的障碍,使得我们往往不屑于耐心的查看关于某个新技术的特点和背景~

这两天也是通过在Apache网站上看Axis2的文档才了解到一些关于REST的东西。整理了一些关于REST的资料和例子,跟大家分享一下~

例子:REST与Axis2的结合

服务器端:HiService
Web服务,提供两个接口
String sayHiTo(String name);
String sayHi();
HiService代码如下:


package com.al;




public class HiService...{




public String sayHi()...{


return "Hi!";


}




public String sayHiTo(String name)...{


return "Hi,"+name;


}


}

配好服务,WSDL可以如下访问:
http://localhost:8080/axis2/services/HiService?wsdl

REST模式可以通过在地址栏里通过如下URL来验证(这是比较简单,也比较直接的例子):http://localhost:8080/axis2/services/HiService/sayHi

注意到,对于Axis的1.0版本,像上面这样(.../HiService/sayHi)访问一个类的方法是做不到的~ 这便是REST这个设计模式的大致想表达的东西~ 为每一个web上的资源通过一个URI来定位~

下面我们通过创建客户端来调用sayHiTo()
HiRESTClient
HiSOAPClient
[两个文件的核心内容其实只相差一行:options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);]

HiSOAPClient代码如下:




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.AxisFault;


import org.apache.axis2.Constants;


import org.apache.axis2.addressing.EndpointReference;


import org.apache.axis2.client.Options;


import org.apache.axis2.client.ServiceClient;




import javax.xml.namespace.QName;


import javax.xml.stream.FactoryConfigurationError;


import javax.xml.stream.XMLOutputFactory;


import javax.xml.stream.XMLStreamException;


import javax.xml.stream.XMLStreamWriter;






/** *//**


* This is a Client progam that accesses 'Hi' web service


*/




public class HiSOAPClient ...{




private static String toEpr = "http://localhost:8333/axis2/services/HiService";






public static void main(String[] args) throws AxisFault ...{




Options options = new Options();


options.setTo(new EndpointReference(toEpr));


options.setTransportInProtocol(Constants.TRANSPORT_HTTP);




// ===================== Here comes the difference =========================


// options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);




ServiceClient sender = new ServiceClient();




sender.setOptions(options);


OMElement result = sender.sendReceive(getPayload());






try ...{


XMLStreamWriter writer = XMLOutputFactory.newInstance()


.createXMLStreamWriter(System.out);


result.serialize(writer);


writer.flush();




} catch (XMLStreamException e) ...{


e.printStackTrace();




} catch (FactoryConfigurationError e) ...{


e.printStackTrace();


}


}








private static OMElement getPayload() ...{


OMFactory fac = OMAbstractFactory.getOMFactory();


OMNamespace omNs = fac.createOMNamespace(


"http://al.com/xsd", "xsd");


OMElement method = fac.createOMElement("sayHiTo", omNs);


OMElement value = fac.createOMElement("name", omNs);


value.addChild(fac.createOMText(value, "Jack 'n Johns"));


method.addChild(value);




return method;


}


}



HiRESTClient代码如下:




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.AxisFault;


import org.apache.axis2.Constants;


import org.apache.axis2.addressing.EndpointReference;


import org.apache.axis2.client.Options;


import org.apache.axis2.client.ServiceClient;




import javax.xml.namespace.QName;


import javax.xml.stream.FactoryConfigurationError;


import javax.xml.stream.XMLOutputFactory;


import javax.xml.stream.XMLStreamException;


import javax.xml.stream.XMLStreamWriter;






/** *//**


* This is a Client progam that accesses 'Hi' web service


*/




public class HiRESTClient ...{




private static String toEpr = "http://localhost:8333/axis2/services/HiService";






public static void main(String[] args) throws AxisFault ...{




Options options = new Options();


options.setTo(new EndpointReference(toEpr));


options.setTransportInProtocol(Constants.TRANSPORT_HTTP);




options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);




ServiceClient sender = new ServiceClient();




sender.setOptions(options);


OMElement result = sender.sendReceive(getPayload());






try ...{


XMLStreamWriter writer = XMLOutputFactory.newInstance()


.createXMLStreamWriter(System.out);


result.serialize(writer);


writer.flush();




} catch (XMLStreamException e) ...{


e.printStackTrace();




} catch (FactoryConfigurationError e) ...{


e.printStackTrace();


}


}








private static OMElement getPayload() ...{


OMFactory fac = OMAbstractFactory.getOMFactory();


OMNamespace omNs = fac.createOMNamespace(


"http://al.com/xsd", "xsd");


OMElement method = fac.createOMElement("sayHiTo", omNs);


OMElement value = fac.createOMElement("name", omNs);


value.addChild(fac.createOMText(value, "Jack 'n Johns"));


method.addChild(value);




return method;


}


}



通过tcpmon工具来查看客户端与服务器端通信内容:
HiSOAPClient调用时的HTTP内容



HiRESTClient调用时的HTTP内容



可以看到REST模式下传递的并非SOAP的Envelope,这里我们是以POST模式发送的~
在Apache官方文档中能够看到REST在SOAP下的解析方式,SOAP Engine将REST的URL重新解析成一个SOAP消息来解析~ 这种扩展REST的方式比较容易理解,让人感觉一个技术的架构扩展方面的总会有alternative的~

就先说这么多了~ 如果大家有疑问,就给我留言吧~

声明:此文不涉及任何组织或机构的机密,作者只是根据开源网站的提供的文档资料进行整理,与广大开发者分享技术带来的乐趣。任何人可以转载此文。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: