Rest-style Web Services

Source: Internet
Author: User
Tags representational state transfer
1. What is rest?

Rest is written by Roy Fielding in his paperElastic tural styles and the design of network-based software ubunturesA term. Rest is the abbreviation of representational state transfer in English. It is translated into "representative State transmission" (see representational state transfer (rest) and data distribution in the SIP/IMS network). Rest can be summarized as follows:   1.1 first, rest is just a style, not a standard
You will not see the W3C putting out a rest specification. You will not see IBM or Microsoft or sun selling a rest developer's toolkit. Why? Because rest is just an unsupported tural style. You can't bottle up that style. You can only understand it, and design your web services in that style.-Roger L. CostelloBuilding Web services the rest way.
  1.2 rest is resource-centric In rest, the Web is considered to be composed of a series of abstract resources, which have different expressions (representational State ). For example, define a resource as photo, which means a photo. It can be an image or a. xml file, which contains some elements that describe the photo. Or an HTML file. And the specific performance can be distributed in different physical locations.   1.3 The purpose of rest is to determine how to make a well-defined WebProgramForward A program can select a hyperlink on a web page (these links represent resources) to make another web page (representational state of an abstract Resource) Return (transfer) to the user, run the program further.   1.4 restful exploitation or extreme dependency on HTTP It uses the logical URI Locate resources In the definition of rest, a Web application always uses a fixed URI to present (or expose) a resource to the external world. Note that 1. a URI corresponds to a resource. 2. The URI here is a logical Uri, A logical URI: [url] http://www.example.com/photo/logo#/url] A physical URI: [url] http://www.example.com/photo/logo.html#/url]   Identify HTTP request headers Information to identify which kind of data the client wants to obtain resources. Let's take a practical example: [Url] http://www.example.com/photo/logo#/url] directs to a resource of the type photo and logo in example.com (which can be considered as a web application. We can access this URI in a browser and see an HTML document, where To display the actual photo. [Url] http://www.example.com/photo/logo#/url?this address can easily be internally processed as a [url] http://www.example.com/photo.jsp in the server? Name = address like logo [/url. Photo. jsp is a dynamic script file on the server. HTML documents are generated based on the name parameter and returned to the browser. Now let's assume that we want to get the XML document of this photo. The XML document contains the photo file name, file size, shooting date, and other information. That is to say, we need to get Data of different expressions of the same resource". For this requirement, we can easily use another URL address to: [url] http://www.example.com/xml/logo#/url#. However, this violates" Uri Uniquely identifies a resource. If we want to obtain multiple representations of the same resource, we need to use more URLs to specify multiple different Uris for a resource. In rest, the same URI is used for both the XHTML document, XML document, and the photo file itself, which is [url] http://www.example.com/photo/logo#/url.pdf. What should I do? It is to distinguish the HTTP request header information to identify which kind of data the client wants to obtain resources. When we access a website using a browser, the browser constructs an HTTP request. This request contains a header containing the type of data that the request accepts. Generally, the accept value in the HTTP request header sent by the browser is */*, that is, accept any type of data returned by the server. If a specific accept parameter is specified, the server can determine the type of data returned by this parameter. Therefore, in an application that uses the rest architecture, you only need to use different HTTP request header information to obtain data of different expressions of the same resource. If you consider adding web services to Web applications, the value of this technology will be reflected. For example, if you write a program, you only need to construct an HTTP request header containing accept: text/XML, and then send the request to [url] http://www.example.com/photo/logo#/url. The returned result is an XML document, rather than data in other resource forms.   In rest In the architecture, different HTTP Request method to process the crud of the Resource (Create, read, update, and delete) Operations When processing requests from clients in a web application, we usually only consider the get and post HTTP request methods. In fact, there are headers, put, delete, and other request methods in HTTP. In the rest architecture, different HTTP request methods are used to process CRUD (create, read, update, and delete) operations on resources: Ø post: Create Ø get: Read Ø put: Update Delete: Delete After such expansion, we can perform crud operations on a resource through the same URI: [Url] http://www.example.com/photo/logo#/url] (read)
Remain as [get] [url] http://www.example.com/photo/logo#/url] [Url] http://www.example.com/photo/logo/create#/url] (create)
Change to [Post] [url] http://www.example.com/photo/logo#/url] [Url] http://www.example.com/photo/logo/update#/url] (updated)
Change to [put] [url] http://www.example.com/photo/logo#/url] [Url] http://www.example.com/photo/logo/delete#/url] (delete)
Change to [delete] [url] http://www.example.com/photo/logo#/url] This further standardizes the use of resource IDs. With the rest architecture, web applications can use consistent interfaces (URIs) to expose resources to the external world and provide semantic-consistent operation services for resources. This is very important for resource-centric Web applications.   2. Restful web services features By setting the HTTP accept type to text/XML, we can express the "concrete State" returned by the resource as data that can be recognized across platforms. This is the basis for Web Services. Restful Web Service has the following features: 2.1 stateless Each request from the client must contain all necessary information, that is, the server cannot obtain any stored context information. The "client-stateless-server" constraint of rest prohibits you from saving the session status on the server. Design with this constraint 1. the system reliability and scalability can be improved. It does not require expensive maintenance and support because the status is not maintained on the server. 2. You can cache resources. The Web high-speed cache can either reside on the client host or the intermediate network high-speed cache server host.   2.2 uniform interface and resource address visible in the HTTP Header By parsing the HTTP head, we can understand the requested resources and methods. Compared with the soap RPC style, We must parse the HTTP body. In this way, some proxy server settings will bring high processing efficiency. All the actions and resources to be accessed in the rest system can be obtained from the h ttp and Uri, which makes the proxy server, Cache Server, and gateway coordinate work well. The resources to be accessed by RPC-mode soap are only known from the URI, and the methods to be called cannot be known from HTTP. They are all hidden in the SOAP message. Similarly, the proxy server in the rest system can also be controlled through HTTP actions (get and post.   2.3 Return the content in general XML format generally, a restful Web Service consumes less transmission bandwidth than a soap RPC web service.
post/order HTTP/1.1 Host: [url] www.northwindtraders.com [/url] Content-Type: text/XML Content-Length: NNNN 098 999 89 3000
 
Post/order HTTP/1.1 HOST: [url] www.northwindtraders.com [/url] Content-Type: text/XML Content-Length: NNNN Soapaction: "urn: northwindtraders.com: Po # updatepo"   <SOAP-ENV: Envelope Xmlns: xsi = "[url] http://www.3366org/5o/xmlschema/instance#/url#" Xmlns: SOAP-ENV = "[url] http://schemas.xmlsoap.org/soap/envelope#/url#" Xsi: schemalocation = "[url] http://www.northwindtraders.com/schema/nposchema.xsd#/url.pdf"> <SOAP-ENV: Body xsi: TYPE = "northwindbody"> <Updatepo> <Orderid> 098 </orderid> <Customernumber> 999 </customernumber> <Item> 89 </item> <Quantity> 3000 </quantity> </Updatepo> SOAP-ENV: Body> SOAP-ENV: envelope>
  2.4 Security Mechanism Rest uses a simple and effective security model. In rest, it is easy to hide a resource. You only need to release its U Ri, but it is also easy to use some security policies on the resource. For example, you can set permissions for each URI for four general interfaces; furthermore, resource-centric Web services are firewall-friendly, because get means get, P ut means put, the administrator can set the resource to read-only by blocking non-GET requests, and the current RPC-based soap model all works on the http post. When using the soap RPC model, the name of the object to be accessed is hidden in the parameters of the method. Therefore, you need to create a new security model.   2.5 unable to be used for transactional services For transactional services, a simple example is banking transactions where users can transfer money from one account to another. Users do not want to directly operate on resources (money, bank accounts, and so on). They just want to tell the bank what they want to achieve and let the bank handle the resources according to their interests. Therefore, we should understand from this article that if we choose a rest or soap RPC-based Web service, we should first consider whether the service is resource-oriented or activity-oriented. - James Snell , Resource-oriented and activity-oriented web Service, Original ENGLISH .   3. JAX-WS Java architecture for XML Web Services, simply put, is a framework for developing web services applications using Java and XML, the current version is 2.0, It is a later version of JAX-RPC 1.1. JAX-WS2.0 Use jaxb2 to process the ing between Java objects and XML. The JAX-WS builds a rest-style terminal through the javax. xml. ws. provider interface.   4. wadl Web application Description Language (wadl) It was proposed by Sun to provide a web application Description Language. Wadl mainly describes a web application
    • Resource list-all Site Resources
    • Relationship between resources-Description of the link between resources
    • All specific methods applied to each resource-HTTP methods applied to each resource, specified input and output, and supported data formats
    • Resource representation-supported MIME types and XML schemas used
  The following is a wadl example that describes the Yahoo News search application.
    • Line 2 starts an application description and defines the namespaces used.
    • Lines 3-6 defines the XML syntax. In this example, two W3C XML schemas are used.
    • Lines 7-11 describes Yahoo News search Web Resources and Their supported HTTP methods.
    • Lines 12-26 describes the 'search' get method..
    • Lines 13-21 description Input.
    • Lines 22-25 describes all possible outputs.
  5. wadl Project Introduction   Glassfish»Web application Description Language Wadl2java A tool that generates Client Side stubs from wadl files. may be used from the command line or as an Apache ant plug-in, see the wadl2java documentation for full details. Wadl2java_yahoo A sample project that uses the wadl2java tool to create stubs for the Yahoo News search service. Includes a simple main method that uses the generated stubs to query for the latest Java news. 6. Recommended Materials [1] Roy Thomas Fielding,Architectural styles and the design of network-based software ubuntures [2]Restwiki [3] Restful tural Style [4] Rest mailing list [5] Building Web services the rest way [6] Resource-oriented and activity-Oriented Web Services [7] Some thoughts about soap versus rest on Security [8] Marc Hadley's blog by wadl [9] Java Web Service Technologies @ sun

This article is from the "sub-blog", please be sure to keep this source http://zhangjunhd.blog.51cto.com/113473/47283

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.