What exactly is RESTful programming?

前端 未结 30 3214
Happy的楠姐
Happy的楠姐 2020-11-21 06:02

What exactly is RESTful programming?

相关标签:
30条回答
  • 2020-11-21 06:24

    The answer is very simple, there is a dissertation written by Roy Fielding.]1 In that dissertation he defines the REST principles. If an application fulfills all of those principles, then that is a REST application.

    The term RESTful was created because ppl exhausted the word REST by calling their non-REST application as REST. After that the term RESTful was exhausted as well. Nowadays we are talking about Web APIs and Hypermedia APIs, because the most of the so called REST applications did not fulfill the HATEOAS part of the uniform interface constraint.

    The REST constraints are the following:

    1. client-server architecture

      So it does not work with for example PUB/SUB sockets, it is based on REQ/REP.

    2. stateless communication

      So the server does not maintain the states of the clients. This means that you cannot use server a side session storage and you have to authenticate every request. Your clients possibly send basic auth headers through an encrypted connection. (By large applications it is hard to maintain many sessions.)

    3. usage of cache if you can

      So you don't have to serve the same requests again and again.

    4. uniform interface as common contract between client and server

      The contract between the client and the server is not maintained by the server. In other words the client must be decoupled from the implementation of the service. You can reach this state by using standard solutions, like the IRI (URI) standard to identify resources, the HTTP standard to exchange messages, standard MIME types to describe the body serialization format, metadata (possibly RDF vocabs, microformats, etc.) to describe the semantics of different parts of the message body. To decouple the IRI structure from the client, you have to send hyperlinks to the clients in hypermedia formats like (HTML, JSON-LD, HAL, etc.). So a client can use the metadata (possibly link relations, RDF vocabs) assigned to the hyperlinks to navigate the state machine of the application through the proper state transitions in order to achieve its current goal.

      For example when a client wants to send an order to a webshop, then it have to check the hyperlinks in the responses sent by the webshop. By checking the links it founds one described with the http://schema.org/OrderAction. The client know the schema.org vocab, so it understands that by activating this hyperlink it will send the order. So it activates the hyperlink and sends a POST https://example.com/api/v1/order message with the proper body. After that the service processes the message and responds with the result having the proper HTTP status header, for example 201 - created by success. To annotate messages with detailed metadata the standard solution to use an RDF format, for example JSON-LD with a REST vocab, for example Hydra and domain specific vocabs like schema.org or any other linked data vocab and maybe a custom application specific vocab if needed. Now this is not easy, that's why most ppl use HAL and other simple formats which usually provide only a REST vocab, but no linked data support.

    5. build a layered system to increase scalability

      The REST system is composed of hierarchical layers. Each layer contains components which use the services of components which are in the next layer below. So you can add new layers and components effortless.

      For example there is a client layer which contains the clients and below that there is a service layer which contains a single service. Now you can add a client side cache between them. After that you can add another service instance and a load balancer, and so on... The client code and the service code won't change.

    6. code on demand to extend client functionality

      This constraint is optional. For example you can send a parser for a specific media type to the client, and so on... In order to do this you might need a standard plugin loader system in the client, or your client will be coupled to the plugin loader solution.

    REST constraints result a highly scalable system in where the clients are decoupled from the implementations of the services. So the clients can be reusable, general just like the browsers on the web. The clients and the services share the same standards and vocabs, so they can understand each other despite the fact that the client does not know the implementation details of the service. This makes possible to create automated clients which can find and utilize REST services to achieve their goals. In long term these clients can communicate to each other and trust each other with tasks, just like humans do. If we add learning patterns to such clients, then the result will be one or more AI using the web of machines instead of a single server park. So at the end the dream of Berners Lee: the semantic web and the artificial intelligence will be reality. So in 2030 we end up terminated by the Skynet. Until then ... ;-)

    0 讨论(0)
  • 2020-11-21 06:24

    RESTful (Representational state transfer) API programming is writing web applications in any programming language by following 5 basic software architectural style principles:

    1. Resource (data, information).
    2. Unique global identifier (all resources are unique identified by URI).
    3. Uniform interface - use simple and standard interface (HTTP).
    4. Representation - all communication is done by representation (e.g. XML/JSON)
    5. Stateless (every request happens in complete isolation, it's easier to cache and load-balance),

    In other words you're writing simple point-to-point network applications over HTTP which uses verbs such as GET, POST, PUT or DELETE by implementing RESTful architecture which proposes standardization of the interface each “resource” exposes. It is nothing that using current features of the web in a simple and effective way (highly successful, proven and distributed architecture). It is an alternative to more complex mechanisms like SOAP, CORBA and RPC.

    RESTful programming conforms to Web architecture design and, if properly implemented, it allows you to take the full advantage of scalable Web infrastructure.

    0 讨论(0)
  • 2020-11-21 06:24

    There is not such notion as "RESTful programming" per se. It would be better called RESTful paradigm or even better RESTful architecture. It is not a programming language. It is a paradigm.

    From Wikipedia:

    In computing, representational state transfer (REST) is an architectural style used for web development.

    0 讨论(0)
  • 2020-11-21 06:25

    RESTful programming is about:

    • resources being identified by a persistent identifier: URIs are the ubiquitous choice of identifier these days
    • resources being manipulated using a common set of verbs: HTTP methods are the commonly seen case - the venerable Create, Retrieve, Update, Delete becomes POST, GET, PUT, and DELETE. But REST is not limited to HTTP, it is just the most commonly used transport right now.
    • the actual representation retrieved for a resource is dependent on the request and not the identifier: use Accept headers to control whether you want XML, HTTP, or even a Java Object representing the resource
    • maintaining the state in the object and representing the state in the representation
    • representing the relationships between resources in the representation of the resource: the links between objects are embedded directly in the representation
    • resource representations describe how the representation can be used and under what circumstances it should be discarded/refetched in a consistent manner: usage of HTTP Cache-Control headers

    The last one is probably the most important in terms of consequences and overall effectiveness of REST. Overall, most of the RESTful discussions seem to center on HTTP and its usage from a browser and what not. I understand that R. Fielding coined the term when he described the architecture and decisions that lead to HTTP. His thesis is more about the architecture and cache-ability of resources than it is about HTTP.

    If you are really interested in what a RESTful architecture is and why it works, read his thesis a few times and read the whole thing not just Chapter 5! Next look into why DNS works. Read about the hierarchical organization of DNS and how referrals work. Then read and consider how DNS caching works. Finally, read the HTTP specifications (RFC2616 and RFC3040 in particular) and consider how and why the caching works the way that it does. Eventually, it will just click. The final revelation for me was when I saw the similarity between DNS and HTTP. After this, understanding why SOA and Message Passing Interfaces are scalable starts to click.

    I think that the most important trick to understanding the architectural importance and performance implications of a RESTful and Shared Nothing architectures is to avoid getting hung up on the technology and implementation details. Concentrate on who owns resources, who is responsible for creating/maintaining them, etc. Then think about the representations, protocols, and technologies.

    0 讨论(0)
  • 2020-11-21 06:25

    Old question, newish way of answering. There's a lot of misconception out there about this concept. I always try to remember:

    1. Structured URLs and Http Methods/Verbs are not the definition of restful programming.
    2. JSON is not restful programming
    3. RESTful programming is not for APIs

    I define restful programming as

    An application is restful if it provides resources (being the combination of data + state transitions controls) in a media type the client understands

    To be a restful programmer you must be trying to build applications that allow actors to do things. Not just exposing the database.

    State transition controls only make sense if the client and server agree upon a media type representation of the resource. Otherwise there's no way to know what's a control and what isn't and how to execute a control. IE if browsers didn't know <form> tags in html then there'd be nothing for you to submit to transition state in your browser.

    I'm not looking to self promote, but i expand on these ideas to great depth in my talk http://techblog.bodybuilding.com/2016/01/video-what-is-restful-200.html .

    An excerpt from my talk is about the often referred to richardson maturity model, i don't believe in the levels, you either are RESTful (level 3) or you are not, but what i like to call out about it is what each level does for you on your way to RESTful

    0 讨论(0)
  • 2020-11-21 06:28

    REST is an architectural style which is based on web-standards and the HTTP protocol (introduced in 2000).

    In a REST based architecture, everything is a resource(Users, Orders, Comments). A resource is accessed via a common interface based on the HTTP standard methods(GET, PUT, PATCH, DELETE etc).

    In a REST based architecture you have a REST server which provides access to the resources. A REST client can access and modify the REST resources.

    Every resource should support the HTTP common operations. Resources are identified by global IDs (which are typically URIs).

    REST allows that resources have different representations, e.g., text, XML, JSON etc. The REST client can ask for a specific representation via the HTTP protocol (content negotiation).

    HTTP methods:

    The PUT, GET, POST and DELETE methods are typical used in REST based architectures. The following table gives an explanation of these operations.

    • GET defines a reading access of the resource without side-effects. The resource is never changed via a GET request, e.g., the request has no side effects (idempotent).
    • PUT creates a new resource. It must also be idempotent.
    • DELETE removes the resources. The operations are idempotent. They can get repeated without leading to different results.
    • POST updates an existing resource or creates a new resource.
    0 讨论(0)
提交回复
热议问题