Blogs On Programming

REST APIs and Implementations

REST APIs and Implementations

by Vibrant Publishers on Jan 01, 2022
Introduction Building a RESTful service is specific to every programming language. There are different libraries and implementations of REST in each programming language. In this post, we are going to look into the details of how Java language implements REST and how we can build RESTful applications in Java. Java and REST REST got into the Java world through the release of JavaEE 6. It included the JAX-RS API that is responsible for REST API creation using Java. JAX-RS follows the REST framework and the HATEOAS principle. The current version of JAX-RS is version 2.0. JAX-RS uses annotations for ease of development and deployment of web services. These annotations are part of the java package javax.ws.rs. The basic annotations provided by JAX-RS are: @Path @GET, @POST, @PUT, @DELETE, @HEAD @Produces @Consumes @PathParam JAX-RS Implementations There are many implementations of JAX-RS available in Java. They show minor variations in their ways of defining and using REST APIs. But the basic procedures remain the same in all of them. A few of those implementations are: Jersey: This is the reference implementation of JAX-RS from Sun Microsystems. This is one of the most widely used libraries for building RESTful services in Java. Spring REST: This REST library is part of the Spring web framework. If you are using the spring framework for building your application, making it restful is very easy. It follows an MVC architecture. RESTeasy: RESTeasy is developed by JBOSS and is part of their application server. This implementation has server-side caching and GZIP compression features. Apache CXF: This implementation of JAX-RS specification is by Apache. It is an open source library available for free download and modification. Restlet: This is again an open source implementation of JAX-RS 1.1 specification. It is a light-weight library that supports major media types and all REST specifications. Serialization Frameworks When we implement REST APIs depending on the request and response types, one would always need to use some kind of object serializers. The following are some of the popular serialization frameworks: JAXB It stands for “Java Architecture for XML Binding”. This library helps in the conversion of java objects into an XML data format and vice versa. One can find this API in the javax.xml.bind package. There are many implementations of JAXB in the form of annotations that can be used in java code for conversions. JACKSON Jackson is a JSON processor that is used widely in the Java world for converting Java objects into JSON document format and vice versa. This implementation is part of the Jersey library. Building a RESTful Application As you can see, there are many choices in the Java world for building a RESTful application. The libraries and implementations listed here can be used with any Java framework. It is our choice to go for a particular library based on our use case and technologies that we are adopting. General steps for building any RESTful services would be: Identify resources and build URIs Select or build representations. Identify method semantics. Select response codes. The below diagram shows a simple technology stack for a RESTful application.         Summary After the introduction of REST in JavaEE from version 6 onwards, a lot of implementations and libraries came into existence. The JAX-RS has got strict implementation principles and guidelines for building RESTful API services. As a java web services developer it is important to understand which library to use and how to build RESTful services adhering to the REST principles.  
Rest Resources, HTTP Methods and Status Codes

Rest Resources, HTTP Methods and Status Codes

by Vibrant Publishers on Dec 29, 2021
Introduction REST stands for REpresentational State Transfer. We use REST while doing web services programming to communicate between client and server. There are many concepts involved in the REST world that are required to know for building a RESTful service. We will have a look at some of those building blocks of RESTful service here. ResourcesEverything in REST is a resource. It can be a document, an image, a collection of objects, information stored in the database, etc. REST requires a Unique Resource Identifier (URI) associated with each resource for it to be accessible over the internet. URI is the endpoint of a given resource. The state of each resource is also an important factor while accessing it. The client-side can use the resource in the best possible way when it knows the type and state of the resource. So REST responses will have a machine-readable explanation about the resource. That is a resource representation. It can be the details of the resource, its format, size, etc. REST and HTTPWe saw resources can be of any type, but there are some rules in the REST world while interacting with resources. REST requests and responses use the HTTP protocol. There are specific HTTP message formats that REST clients have to follow while sending a request. The response from the server also will be in a certain message format that a REST client can decode.There are eight different message formats that HTTP has defined. The following are the commonly used four methods. GET: Get a representation of a given resource. DELETE: Destroy the specified resource. POST: Create a new resource, based on the given representation. PUT: Update the state of a given resource with the one described in the given representation. The main aspects of choosing these methods are because of its adherence to the REST specification (RFC 2616). The two main properties in RFC 2616 are safety and idempotency. Safety here means the ability of the client to make requests without altering the state of a given resource. Idempotency means the effect of doing an operation will be the same for single or multiple times. Request Response Structure Now we will see how these methods are used in an HTTP request and response in a RESTful way. A REST client library will create a REST API request in the following format: The API response, in turn, will have the following format:Following is a sample GET request:Method: GET URL: http://api.clothshop.com/shirtsWhich will provide the following response: HTTP/1.1 200 Content-Type: application/json { “id”: 10, “name”: “Lee Shirt”, “color”: “yellow”, “price”: “$30” }HTTP Status CodesWhenever we do a REST API request to the server, the server will return a response with an HTTP status code. Each status code will tell a brief about the response from the server. These status codes will be part of the HTTP header which the client will decode and understand. These are wrapped with HTML code and all HTML /HTML5 browsers are capable of understanding these HTTP codes. The following table summarizes the major status codes and their meaning. HTTP Status Codes Summary 200 OK. Request successful. 201 Resource Created. 204 No content found. 400 Bad Request. 401 Unauthorised. 404 Resource not found. 405 Method not allowed 500 Internal Server Error. Summary There are multiple building blocks in the REST framework that makes web services communication happen over the internet. HTTP protocol is one of the main blocks upon which the entire REST communication is built. While designing a REST API it is important to consider the nature of resources that we are planning to expose as APIs. Also it is important to know various HTTP methods and status codes that will help us in the process of accessing these resources at the client side.