Simple rest Framework Implementation

Source: Internet
Author: User

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:

  1. The rest architecture is just an idea and does not limit any technology or language.
  2. The essence of rest is HTTP call, which is used to reduce the Coupling Degree between applications.
  3. 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
  1. @ Override
  2. Public void Init (servletconfig config) throws servletexception {
  3. // 1. Load the corresponding rest service from init. If there is a spring framework or something else, the principles are the same.
  4. String serviceclass = config. getinitparameter ("service-class ");
  5. If (serviceclass! = NULL ){
  6. System. Out. println ("Rest service:" + serviceclass );
  7. String [] classes = serviceclass. Split (",");
  8. Try {
  9. For (string classname: classes ){
  10. Class newclass = Class. forname (classname );
  11. Object newobject = newclass. newinstance ();
  12. If (newobject instanceof irestservice ){
  13. Irestservice restservice = (irestservice) newobject;
  14. Services. Put (restservice. geturi (), restservice );
  15. System. Out. println ("load rest service:" + newobject. getclass (). getname () + ", uri =" + restservice. geturi ());
  16. }
  17. }
  18. } Catch (exception e ){
  19. System. Out. println ("loading the rest service error:" + E. getmessage ());
  20. }
  21. }
  22. // 2. Load the interceptor
  23. String interceptorclas = config. getinitparameter ("Interceptor-class ");
  24. If (interceptorclas! = NULL ){
  25. System. Out. println ("Interceptor:" + serviceclass );
  26. String [] classes = interceptorclas. Split (",");
  27. Try {
  28. For (string classname: classes ){
  29. Class newclass = Class. forname (classname );
  30. Object newobject = newclass. newinstance ();
  31. If (newobject instanceof irestinterceptor ){
  32. Irestinterceptor interceptor = (irestinterceptor) newobject;
  33. Interceptors. Add (interceptor );
  34. System. Out. println ("load rest Interceptor:" + newobject. getclass (). getname ());
  35. }
  36. }
  37. } Catch (exception e ){
  38. System. Out. println ("An error occurred while loading the rest Interceptor:" + E. getmessage ());
  39. }
  40. }

Forwarding implementation

[Java]View plaincopy
  1. @ Override
  2. Protected void Service (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {
  3. // Process HTTP requests
  4. Response. setcontenttype ("text/html; charset = GBK ");
  5. Response. setcharacterencoding ("GBK ");
  6. Response returnresponse = new response (); // The final returned result, which can be in JSON or XML format
  7. Try {
  8. // 1. Get the requested URI
  9. String uri = request. getrequesturi ();
  10. // System. Out. println ("uri =" + URI );
  11. // 2. obtain the corresponding restservice
  12. Irestservice service = services. Get (URI );
  13. If (service! = NULL ){
  14. // 2.0 check whether the service meets the current environment
  15. //--------------
  16. // 2.1 construct the corresponding request and response context
  17. Genericrestrequest restrequest = new genericrestrequest (request );
  18. Genericrestresponse restresponse = new genericrestresponse (response );
  19. // 2.2 fill in environment variables and the like
  20. Restrequest. setrestservice (service );
  21. Restresponse. setrestservice (service );
  22. // 2.3 execute the interceptor
  23. For (irestinterceptor Interceptor: interceptors ){
  24. Interceptor. handlerest (restrequest, restresponse );
  25. }
  26. // 2.4 execute the service
  27. Service. Service (restrequest, restresponse );
  28. // 2.5
  29. If (restresponse. getresponsedata ()! = NULL ){
  30. Returnresponse = restresponse. getresponsedata ();
  31. }
  32. } Else {
  33. Throw new exception ("corresponding rest service not found:" + URI );
  34. }
  35. } Catch (exception e ){
  36. E. printstacktrace ();
  37. Returnresponse. adderror ("doaction", E. getmessage ());
  38. }
  39. Response. getwriter (). Write (returnresponse. tojson ());
  40. }

 

 

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
  1. If (data. Code = 200) {// if it is successful, return
  2. VaR users = data. Messages. List; // This is returned by the rest service.
  3. VaR html = "display data in ol mode: <br/> <ol>"
  4. For (VAR I = 0; I <users. length; I ++ ){
  5. HTML + = "<li>" + users [I]. Name + "</LI> ";
  6. }
  7. HTML + = "</OL> ";
  8. $ ("# Idresult" ).html (HTML );
  9. } Else {
  10. // Error, or something else
  11. Alert (data. Messages. doaction );
  12. }

 

 

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.