Light, who will write the service scheme--rest and Json__json

Source: Internet
Author: User
1.REST 1.1 Origin

N years ago, when it comes to cross-platform service solutions, Daniel thinks of "Socket Server", and the soldiers retreat to the corner of the conference room.
A few years ago, when it came to cross-platform service scenarios, even customers thought of "Web service."
Now, it's an individual who can, in a few minutes, use the rest style to write down the client and server side of a service. 1.2 First
sight

Rest is a word first, and then represents a service delivery pattern. Well, Sages said, any service agreement, can be broken into a transport protocol, service mode, data format three-dimensional expression, that rest is dependent on HTTP as a transport mechanism, Request-reponse mode, data is a predefined arbitrary format.

As a result, clients in any language use an HTTP library to access a URL, write the request information in XML or JSON or a pure string, and place it in the post entity. The server also arbitrarily implements a servlet or even jsp/asp/php, receiving a request from the client, returning the result of the xml/json/string.

So easy, the mind is not immediately think of the way to achieve. In Java, a POST request is sent using Apache httpclient.

HttpClient httpclient = new HttpClient ();
Entityenclosingmethod method = new Postmethod (URL); Method.setrequestbody (fooxml);
Method.setrequestheader ("Content-type", "Application/xml; Charset=utf8");
Httpclient.executemethod (method);
String BODY = method.getresponsebodyasstring ();

Another Xml/json operation library, which is highly recommended for Codehaus XStream, is a beautiful conversion between Xml/json and Java objects. Much more convenient than other heavy-duty XML binding scenarios, the following is the code that XML and Java objects go with each other.

XStream xs = new XStream ();
String xml = Xs.toxml (foo);
Foo foo = (foo) xs. FromXml (XML);

. NET is simpler, with both HTTP and XML libraries in their own.
This is the most attractive place, is rest, writing service is no longer a framework-level thing, no longer need configuration files and callback functions, as long as the knowledge of a few APIs, and even APIs do not, managed DIY out of service. Infoq's article is good: simple JAVA and. NET SOA Interoperability
1.3 doctrine

Rest is a simple, message-oriented (resource) interaction that gradually replaces RPC. Real rest is a proposition that defines an Internet ID for all "things" and joins them. A very good idea.

<order self= ' http://example.com/customers/1234 ' >
	<amount>23</amount>
	<product ref= ' http://example.com/products/4554 '/>
</order>

The first three methods that define Put/get/delete/post are idempotent, so there is no need for a specific API between Internet applications.
For such services as "approval", there is a lack of direct ability to express, can only engage in a call approval business resources, post representatives to initiate approval, delete representative revocation, although awkward, but always pass. Multiple representations of resources.
The client initiates a request, such as Application/vnd.mycompany.customer+xml, Accept:text/x-vcard, that the server renders the same resource as a different return. No State communication.
The state is either placed in the resource state or saved on the client. This allows customers to not rely on server-side instances, and the server can expand or reboot at will. The natural use of HTTP compression and caching mechanism
The HTTP compression mechanism, as long as the client says oneself accept Encoding:gzip,server can compress the transmission.
Use Etags to reduce Web application bandwidth and load (InfoQ)

ETag principle is very simple, is the server in response can bring a ETag, the next user's header can bring a If-none-match:etag ' s value, the server Judge ETag, if not changed, return http/1.x 304 Not Modified.
Here are two algorithms to achieve a different effect of the simple algorithm, the page returns the content of the hashcode as a etag, the server is still calculated, the final page, and hash comparison, if the same as the customer's ETag does not return 304, this algorithm is simple, mainly save data transmission time. Complex algorithm, set the version number for the page, and use the version number as the ETag. Set up the pages affected by resource changes on the server side, such as using Hibernate listener, to increase the version number of all potentially affected pages when data is changed or deleted.  This algorithm is relatively complex, but at the same time saves the server computation time and transmission time.
1.4 Price

Of course, the simplicity of rest also has simple costs, such as lack of transaction, reliability, Ws-address, UDDI and other mechanisms. But these mechanisms are not much used in the Orthodox WebService world. For pure webservice that do not use any additional mechanism, you can consider using rest writing, or designing your own protocols like a DIY transaction.

It is not yet conclusive that the customer will need to interpret payload, or rely on the SDK provided by the server, and not generate DTO,WADL from the direct WSDL.

Finally, rest, as a service solution, can be used as a Web application MVC scheme, such as CETIA4 (https://cetia4.dev.java.net/) to challenge the alternative to the traditional MVC framework, but I think after a bunch of frames, Simple is gradually lost meaning, plus recently did not engage in web applications, spend half a day after reading its tutorial documents, no longer concerned. 2.JSON

Introduction to JSON (DEV2DEV). If you have large data transfers,JSON (JavaScript Object notation)is a simplification of XML, especially complex XML in soap. Such as:

{' product ': {' name ': ' Banana ', ' id ': ' 123 ', ' price ': ' 23.0 '}}

     Each language has n JSON interpreters. XStream also uses jettison as driver, supporting serialization of Java objects and JSON, and changing parameters to jettision when creating XStream objects, and other operations like XML, see JSON Tutorial

    xstream xstream = new xstream (new  Jettisonmappedxmldriver ()); 

    coincidentally, Apache CXF (XFire) also uses the JSON format for Jettison support Web service, as detailed in its JSON Support.

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.