<Tech>Brunch


Building REST Web Service with Spring 5

Representational State Transfer (REST) is an architectural style that specifies constraints, such as the uniform interface, that if applied to a web service induce desirable properties, such as performance, scalability, and modifiability, that enable services to work best on the Web.
In the REST architectural style, data and functionality are considered resources and are accessed using Uniform Resource Identifiers (URIs), typically links on the Web. The resources are acted upon by using a set of simple, well-defined operations. The REST architectural style constrains an architecture to a client/server architecture and is designed to use a stateless communication protocol, typically HTTP.
In the REST architecture style, clients and servers exchange representations of resources by using a standardized interface and protocol.





Crucial features of REST architecture which makes it simple, lightweight, and fast:

Resource identification through URI: A RESTful web service exposes a set of resources that identify the targets of the interaction with its clients. Resources are identified by URIs, which provide a global addressing space for resource and service discovery.

Uniform interface: Resources are manipulated using a fixed set of four create, read, update, delete operations: PUT, GET, POST, and DELETE.
PUT creates a new resource, which can be then deleted by using DELETE. It should be used to update a resource.
GET retrieves the current state of a resource in some representation.
POST transfers a new state onto a resource. Calling POST on the same resource with same request won’t be a nice idea. That is why, POST is preferred to create a new resource and PUT for updating the resource. Calling PUT more than once is safe.
However, both PUT and POST can create new resources.

In REST architectural style, resources are decoupled from their representation so that their content can be accessed in a variety of formats, such as HTML, XML, plain text, PDF, JPEG, JSON, and others. Metadata about the resource is available and used, for example, to control caching, detect transmission errors, negotiate the appropriate representation format, and perform authentication or access control.

Stateful Interactions: Every interaction with a resource is stateless; that is, request messages are self-contained. Stateful interactions are based on the concept of explicit state transfer. Several techniques exist to exchange state, such as URI rewriting, cookies, and hidden form fields.

What is JAX-RS?

JAX-RS is a specification for implementing REST web services in Java, currently defined by the JSR-370. It is part of the Java EE technologies, currently defined by the JSR 366.
The JAX-RS API uses Java programming language annotations to simplify the development of RESTful web services. We can decorate Java programming language class files with JAX-RS annotations to define resources and the actions that can be performed on those resources.
JAX-RS annotations are runtime annotations; therefore, runtime reflection will generate the helper classes and artifacts for the resource.

There are many implementations of JAX-RS available. Jersey (shipped with GlassFish and Payara) is the JAX-RS reference implementation, however there are other implementations such as RESTEasy (shipped with JBoss EAP and WildFly) and Apache CXF (shipped with TomEE and WebSphere).

I have used Spring 5 to develop a sample REST web service, which covers all the HTTP operations, GET, PUT, POST, DELETE.
The Spring Framework is not a JAX-RS implementation and can be seen as a Spring alternative to the JAX-RS stardard. It is a full framework that allows you to create Java enterprise applications. The REST capabilities are provided by the Spring MVC module. It is not a JAX-RS implementation and can be seen as a Spring alternative to the JAX-RS stardard.



Why my preference was to go with Spring for building REST Web service?

  • It provides “full” stack, not just REST facilities.
  • It provides Dependency injection, AOP, Transactions and much more. My application will be better scalable in this way.
  • It supports pluggable view templates such as JSP, freemarker, velocity etc.

Find the code for the Rest web service developed with Spring 5 at my GitHub repo below:
https://github.com/anshulgammy/restwswithspring



Images in the post: Courtesy Google Images Search