In his paper, he introduced REST as a basic concept of the current Web system structure. He proposed the following standards for REST: 1. a group of constraints for modeling modern Web system structures. 2. the REST principle has been used
Introduction
In Roy Fielding's paper, he described REST as a basic concept of the current Web system structure. He proposed the following standards for REST:
1. a group of constraints for modeling modern Web system structures.
2. the REST principle has been applied to HTTP and URI specifications.
3. it is visible in the development of HTTP.
REST is not a protocol, but a system structure style, which is a very important difference.
For Web services, W3C formally defines Web services as follows:
"A Web service is a software system identified by a URI. XML is used to define and describe its public interfaces and bindings. Other software systems can invent its definition. Then, these systems can interact with the Web service according to the pre-determined method, and apply XML-based messages transmitted over the Internet ."
Common sense tells us that the Web service is mainly used for communication between the computer and the computer, rather than between the computer and the user. REST-based Web services are Web services created by applying the structure style of the REST system. The next section uses an example to illustrate how to build a REST-based Web service. To grasp this content, you must first understand Ajax, which is very important. (If you are a beginner in Ajax, please refer to the reference materials for valuable information links .)
Create a REST-based Web service
To create a REST-based Web service, you must first determine all the openly available resources as a Web service. Some resource examples include the employee list, employee details, purchase orders, and so on. In REST, each type of Resource is identified by a unique Resource Identifier (URI. You need to determine a unique URI for each resource. For example, the employee list can be identified as follows: http://www.employee-details.com/employees-list. You can use the following URI to identify an employee: http://www.employee-details.com/employees/01234.
Apply HTTP to GET, PUT, POST, and DELETE to retrieve and correct your resources. Provide some hyperlinks in your resource performance to provide more relevant information. Specify a pattern for the request and response data of these resources, which requires PUT and POST control.
Implement REST-based Web services
You can use HTTP Servlet to implement REST-based Web services. This article uses a virtual service to demonstrate the implementation process, which provides specific information about the company's employees. The employee list resource is represented by a logical URI, http: // localhost: 9080/AJAX_REST_Demo/RESTDemoServlet/employee-list. When this service is called through http get, it returns the list of employees as shown in listing 1.
List 1. list of employees who call HTTP GET
<? Xml version = '1. 0' encoding = 'utf-8'?>
<P: Employees xmlns: p = 'http: // www.employee-details.com '>
<Employee id = '000000' href = '/employees/6666'/>
<Employee id = '000000' href = '/employees/6666'/>
<Employee id = '000000' href = '/employees/6666'/>
<Employee id = '000000' href = '/employees/6666'/>
</P: Employees>
Similarly, an employee's specific information can be expressed by a logical URI, such as http: // localhost: 9080/AJAX_REST_Demo/RESTDemoServlet/employee/0124. When this service is called through http get, it returns the employee details shown in listing 2.
List 2. apply http get to call employee details
<? Xml version = '1. 0' encoding = 'utf-8'?>
<EmpDetail xmlns: p = 'http: // www.employee-details.com '>
<Emp-ID> 00345 </Emp-ID>
<Name> David Henry </Name>
<Department> Finance </Department>
</P: EmpDetail>
Listing 3 shows the Servlet code. Among them, all controllers adopt hard encoding methods, but they can be easily extended to interact with the database to become a real-time, REST-based service.
Listing 3. Servlet code
Public class RESTDemoServlet extends HttpServlet implements Servlet {
/* (Non-Java-doc)
* @ See javax. servlet. http. HttpServlet # HttpServlet ()
*/
Map map = new HashMap ();
/* (Non-Javadoc)
* @ See javax. servlet. GenericServlet # init ()
*/
Public void init () throws ServletException {
// TODO Auto-generated method stub
Super. init ();
Employee emp0 = new Employee ('David ', 'Finance ');
Employee emp1 = new Employee ('Smith ', 'healthcare ');
Employee emp2 = new Employee ('Adam ', 'information technology ');
Employee emp3 = new Employee ('Stephen ', 'Life Sciences ');
Map. put ('20140901', emp0 );
Map. put ('20140901', emp1 );
Map. put ('20140901', emp2 );
Map. put ('20140901', emp3 );
}
}
In the next section, you can understand how to write an Ajax client for this REST-based Web service.
Compiling Ajax clients for REST-based Web services
As mentioned above, Ajax represents Asynchronous JavaScript XML. It is also known as the xml http technique. In Ajax, the core technique is to achieve asynchronous communication with the server without page refreshing. The XMLHTTPRequest object supports asynchronous GET, POST, PUT, and DELETE operations on the server. This does not show any content to the user. In other words, the status message is not displayed. You may consider state changes to specify a handler method and will be notified when the following request is generated:
Initialization
Start
In the returned process
Complete
Listing 4 shows an Ajax-based HTML page code, which can be used as the REST-based Web service client:
Listing 4. Ajax-based HTML page code
<SCRIPT language = 'javascript 'type = 'text/javascript '>
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.