整理了一点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的~
就先说这么多了~ 如果大家有疑问,就给我留言吧~
声明:此文不涉及任何组织或机构的机密,作者只是根据开源网站的提供的文档资料进行整理,与广大开发者分享技术带来的乐趣。任何人可以转载此文。
这两天也是通过在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的~
就先说这么多了~ 如果大家有疑问,就给我留言吧~
声明:此文不涉及任何组织或机构的机密,作者只是根据开源网站的提供的文档资料进行整理,与广大开发者分享技术带来的乐趣。任何人可以转载此文。
相关文章推荐
- freeimage例子资料整理
- 整理的一点SQL资料,欢迎补充
- 一点SICP(Structure and Interpretation of Computer Programs) 资料 (转载加整理)
- 整理资料看到的一点资料
- 整理一点关于Lucene的学习资料, 方便自己与别人查看
- 关于红外遥控的一点资料整理
- 整理的一点MD5资料(第一部分)
- REST调查资料整理
- 关于Field Exit 整理的一点资料
- 操作DataTable中动态的行,一点收集整理资料。
- 【旧资料整理】初级算法-单/双链表逆序一点改进
- 最近在学ExtJs....一种很酷的技术....我在这也整理了一点入门的资料
- 机器学习的一点资料整理
- 整理的一点MD5的资料(第二部分)
- Nginx+Tomcat+其他存储 负载均衡配置和session共享资料整理及例子
- 关于文档写作、幻灯片制作以及资料整理的一点见解
- SFTP使用资料整理及例子
- 工作那些事(五)谈谈项目资料整理和积累
- SNMP详细说明 在网上收集整理的资料 发出来给大家共享
- android开发相关资料整理【2011-09-02】