SERVER-SIDE DEVELOPMENT PART 2 REST
In this article, we will discuss about Server-side Development part 2 REST. REST stand for Representational State Transfer. It is an architectural style for the service of web. Same as the previous article, its divided in to four parts.
1. Introduction to REST
2. Elements of REST style
3. RESTful URLs
4. Use MVC with REST & RESTful web services
Then let’s see how resource based nature of the REST style. As I mention in above, also REST is used as basis for HTTP 1.1 development.
•Thus the WWW including servers, proxies, etc…
•REST is
– Resource-based
– Focuses on Representations (of data/information/resources)
– Derived using Six Constraints
•Things vs Actions
•Nouns vs Verbs
•Identified by URIs
•Multiple URIs may refers to same resource
- But different according to the HTTP verb used (as like CRUD
operation on same student resource)
•Separate from their representations
- Resource may be represented as request content type either JSON or XML etc
Next will see what representations in REST style are.
- It defines how resources get manipulated and presented
- Part of the resource state is transferred between client and server
- Typically JSON or XML
- Or even can be a web page, file, image, etc…
Then will discuss the constraints of REST, indicating their use in the domain of web,
1) Client-server
• The REST is for explicitly for networked distributed systems, which are based on the client-server style
2) Layered System
• A client doesn’t need to know whether it is connected directly to the end server, or to an intermediary along the way.
• Intermediary servers may improve system scalability by enabling load-balancing and by providing shared caches.
• Layers may also enforce security policies.
3) Stateless
• One client can send multiple requests to the server
• Each request is independent
• Every request must contain all the necessary information so that the server can understand it and process it accordingly.
• The server must not hold any information about the client state.
• Any state information must stay on client – such as sessions.
4) Cacheable
• As many clients access the same server, often they may request the same resources
• It is necessary that these responses might be cached, avoiding unnecessary processing, which may affect performance.
Then we move on to the second part of the article, Elements of REST style.
Data
Key aspect of REST is the state of the data elements, its components communicate by transferring representations of the current or desired state of data elements
•REST manages data in the following ways:
• render the data(traditional client-server style) where it is located and send a fixed-format image to the recipient,
• encapsulate the data(mobile object style) with a rendering engine and send both to the recipient or,
• send the raw data to the recipient along Next will move to contemporary examples of different types of implementations for the elements of REST style,
| DATA | EXAMPLES |
| resource | Conceptual target of a hypertext reference |
| resource identifier | URL, URN |
| representation | HTML document, JPEG image, XML dataset |
| Representation metadata | Media type, last-modified time |
| resource metadata | Source link, alternates |
| control data | if-modified-since, cache-control |
Components
•Software that interacts with one another
•Communicate by transferring representations of resources through a standard interface rather than operating directly upon there source itself
•Used to access, provide access to, or mediate access to resources.
| COMPONENT | EXAMPLES |
| User agent | Browser-based-application |
| Origin server | Server-side application |
| Proxy | CERNProxy, NetscapeProxy, Gauntlet |
| Gateway | Squid,CGI,ReversePRoxy |
Connector
- Represent activities involved in accessing resources and transferring representations.
- REST encapsulates different activities of accessing and transferring representations into different connector types
- Connectors are abstract interfaces for component
| CONNECTOR | Examples |
| Client | Browser |
| Server | Web server |
| Cache | Browser cache, Akamai cache network |
| Resolver | DNS lookup, DOI lookup |
| Tunnel | SOCKS, SSL after HTTP CONNECT |
Then will discuss how to define the API of RESTful web services using RESTful URLs,
Request = URL + HTTP Verbs (GET,POST,PUT,DELETE)
• URL is the key technique in RESTful communication
• The API of the RESTful service is a set of URLs
• The resource is determined by the URL segments
• The CRUD operations are determined by the HTTP verb
Operation=Read, Verb=GET
•Collection
•SLIIT.com/students/
•SLIIT.com/students/DS
•SLIIT.com/students/DS/IT123456/posts
•Single item
•SLIIT.com/students/IT123456
•Blog.com/posts/pid15948
•Multiple items
•SLIIT.com/students/IT123456;IT456456;IT998877
Operation=Create, Verb=POST
•Single item (single item in the payload)
•SLIIT.com/students/
•SLIIT.com/students/DS
•SLIIT.com/students/DS/IT123456/posts
•Multiple items
•Same URL, items will be in the payload
Operation=Update, Verb=PUT
•Collection (data in the payload)
•SLIIT.com/students/
•SLIIT.com/students/DS
•SLIIT.com/students/DS/IT123456/posts
•Single item
•SLIIT.com/students/IT123456
•Blog.com/posts/pid15948
•Multiple items
• SLIIT.com/students/IT123456;IT456456;IT998877
Operation=Delete, Verb=DELETE
•Collection
•SLIIT.com/students/
•SLIIT.com/students/DS
•SLIIT.com/students/DS/IT123456/posts
•Single item
•SLIIT.com/students/IT123456
•Blog.com/posts/pid15948
•Multiple items
•SLIIT.com/students/IT123456;IT456456;IT998877
Now let’s move on to the JAX-RS API and its implementations,
- JAX-RS is a java programming language API that provides support in creating web services according to the REST architectural pattern.
- There are two implementations
- Jersery
- It is an open source framework for developing RESTful Web Services in Java. It provides support for JAX-RSAPIs and serves as a JAX-RS Reference Implementation.
- RESTeasy
- RESTEasy is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It is a fully certified and portable implementation
Then let’s Identify the annotations in JAX-RS, explaining their use,
@Path
The @Path annotation’s value is a relative URI path indicating where the Java class will be hosted: for example, /helloworld. You can also embed variables in the URIs to make a URI path template. For example, you could ask for the name of a user and pass it to the application as a variable in the URI:/helloworld/{username}.
@GET
The @GET annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP GET requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.
@POST
The @POST annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP POST requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.
@PUT
The @PUT annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP PUT requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.
@DELETE
The @DELETE annotation is a request method designator and corresponds to the similarly named HTTP method. The Java method annotated with this request method designator will process HTTP DELETE requests. The behavior of a resource is determined by the HTTP method to which the resource is responding.
So I hope you got some idea about topic REST (Representational State Transfer). Will meet in the next article.
REFERENCES
Innovative Mobile Solutions
Transform Ideas into Mobile Apps
Mobile Apps Tailored for Success
Seamless Mobile Experiences
Expert Mobile App Development