Source code:Http://download.csdn.net/source/1662193
1. Understand rest
The rest software architecture was first proposed by Dr. Roy Thomas Fielding in 2000. He depicts the blueprint for developing Internet-based network software. The rest software architecture is an abstract concept. It is an ultra-media distributed system designed to implement the internet.Action Guide. This idea can be achieved using any technology. HTTP is the most famous software architecture. Generally, we also write rest as rest/HTTP. In practice, we often regard rest as an HTTP-based rest software architecture, or take rest and HTTP as an equivalent concept.
For more information, see:
Http://www.infoq.com/cn/articles/rest-architecure
Http://www.kuqin.com/system-analysis/20080515/8518.html
Rest request process
Personal summary:
- The rest architecture is just an idea and does not limit any technology or language.
- The essence of rest is HTTP call, which is used to reduce the Coupling Degree between applications.
- A good rest architecture should have a unified representation and data format, which can effectively organize various resources and effectively control them.
2. Implement the rest Architecture
1. Framework Design
2. Interface Definition
Irestrequest: used to represent rest requests
Irestresponse: used to indicate the rest response
Irestinterceptor: used to represent the rest interceptor
Restexception: used to indicate a rest exception.
3. Main Implementation Code
Initialization code, using the servlet init
[JavaScript]View plaincopy
- @ Override
- Public void Init (servletconfig config) throws servletexception {
- // 1. Load the corresponding rest service from init. If there is a spring framework or something else, the principles are the same.
- String serviceclass = config. getinitparameter ("service-class ");
- If (serviceclass! = NULL ){
- System. Out. println ("Rest service:" + serviceclass );
- String [] classes = serviceclass. Split (",");
- Try {
- For (string classname: classes ){
- Class newclass = Class. forname (classname );
- Object newobject = newclass. newinstance ();
- If (newobject instanceof irestservice ){
- Irestservice restservice = (irestservice) newobject;
- Services. Put (restservice. geturi (), restservice );
- System. Out. println ("load rest service:" + newobject. getclass (). getname () + ", uri =" + restservice. geturi ());
- }
- }
- } Catch (exception e ){
- System. Out. println ("loading the rest service error:" + E. getmessage ());
- }
- }
- // 2. Load the interceptor
- String interceptorclas = config. getinitparameter ("Interceptor-class ");
- If (interceptorclas! = NULL ){
- System. Out. println ("Interceptor:" + serviceclass );
- String [] classes = interceptorclas. Split (",");
- Try {
- For (string classname: classes ){
- Class newclass = Class. forname (classname );
- Object newobject = newclass. newinstance ();
- If (newobject instanceof irestinterceptor ){
- Irestinterceptor interceptor = (irestinterceptor) newobject;
- Interceptors. Add (interceptor );
- System. Out. println ("load rest Interceptor:" + newobject. getclass (). getname ());
- }
- }
- } Catch (exception e ){
- System. Out. println ("An error occurred while loading the rest Interceptor:" + E. getmessage ());
- }
- }
Forwarding implementation
[Java]View plaincopy
- @ Override
- Protected void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
- // Process HTTP requests
- Response. setcontenttype ("text/html; charset = GBK ");
- Response. setcharacterencoding ("GBK ");
- Response returnresponse = new response (); // The final returned result, which can be in JSON or XML format
- Try {
- // 1. Get the requested URI
- String uri = request. getrequesturi ();
- // System. Out. println ("uri =" + URI );
- // 2. obtain the corresponding restservice
- Irestservice service = services. Get (URI );
- If (service! = NULL ){
- // 2.0 check whether the service meets the current environment
- //--------------
- // 2.1 construct the corresponding request and response context
- Genericrestrequest restrequest = new genericrestrequest (request );
- Genericrestresponse restresponse = new genericrestresponse (response );
- // 2.2 fill in environment variables and the like
- Restrequest. setrestservice (service );
- Restresponse. setrestservice (service );
- // 2.3 execute the interceptor
- For (irestinterceptor Interceptor: interceptors ){
- Interceptor. handlerest (restrequest, restresponse );
- }
- // 2.4 execute the service
- Service. Service (restrequest, restresponse );
- // 2.5
- If (restresponse. getresponsedata ()! = NULL ){
- Returnresponse = restresponse. getresponsedata ();
- }
- } Else {
- Throw new exception ("corresponding rest service not found:" + URI );
- }
- } Catch (exception e ){
- E. printstacktrace ();
- Returnresponse. adderror ("doaction", E. getmessage ());
- }
- Response. getwriter (). Write (returnresponse. tojson ());
- }
Download prototype source code:Http://download.csdn.net/source/1662193
Iii. Data Format
Let's take a look at an article previously written: http://blog.csdn.net/maoxiang/archive/2008/06/25/2584282.aspx, improving the UI interaction design of form submitted data
The data format is defined as follows:
JSON format:
{
Code: 200 | 302 | 403 | 500,200 indicates normal, 302 indicates jump, 403 indicates verification code is required, and 500 indicates exception
Messages: {// transmitted data
[Key: value]
}
XML format:
<Response>
<Code> 200 | 302 | 403 | 500 </code>
<Messages>
<Key> </key>
<Value> </value>
</Messages>
</Response>
Example:
{"Code": 200, "error": false, "messages": {"list": [{"name": "user0" },{ "name ": "user1" },{ "name": "user2" },{ "name": "user3" },{ "name": "user4"}]}, "OK": True, "Redirect": false, "verify": false, "version": 2}
Using JavaScript to parse the JSON format is much simpler:
[JavaScript]View plaincopy
- If (data. Code = 200) {// if it is successful, return
- VaR users = data. Messages. List; // This is returned by the rest service.
- VaR html = "display data in ol mode: <br/> <ol>"
- For (VAR I = 0; I <users. length; I ++ ){
- HTML + = "<li>" + users [I]. Name + "</LI> ";
- }
- HTML + = "</OL> ";
- $ ("# Idresult" ).html (HTML );
- } Else {
- // Error, or something else
- Alert (data. Messages. doaction );
- }
Iv. Practical Application
1. Pacific female search system Co., http://shop.pclady.com.cn.
2. Pacific female filter guest system http://blog.pclady.com.cn
3. Pacific female cosmetics database Co., http://cosme.pclady.com.cn.