top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

REST Webservices in JAVA

In general a service that can be accessed over the network is called web service. It is a client server application , where two machines exchange information using a set of standard protocols.







There are two main java web services API: JAX-WS and JAX-RS. JAX-WS is used for Writing SOAP webservices whereas JAX-RS is used to write REST webservices.






REST vs SOAP :

REST APIs is a flexible, scalable, and inclusive way to connect components and integrate applications .REST is a Set of Guidelines but SOAP is protocol based. JSON and HTML are optimized for the internet, whereas XML is bulky and slow. Hence REST applications perform better especially in mobile apps. REST services are simple to write , test , debug and use which makes them versatile compared to SOAP webservices.




REST Architectural Elements:

The Representational State Transfer (REST) style is an abstraction of the architectural elements within a distributed hypermedia system. REST ignores the details of component implementation and protocol syntax in order to focus on the roles of components, the constraints upon their interaction with other components, and their interpretation of significant data elements.

Division of State and Functionality: State and functionality are divided into distributed resources. This is because every resource has to be accessible via normal HTTP commands. That means a user should be able to issue the GET request to get a file, issue the POST or PUT request to put a file on the server, or issue the DELETE request to delete a file from the server.

Stateless, Layered, Caching-Support, Client/Server Architecture: A type of architecture where the web browser acts as the client, and the web server acts as the server hosting the application, is called a client/server architecture. The state of the application should not be maintained by REST. The architecture should also be layered, meaning that there can be intermediate servers between the client and the end server. It should also be able to implement a well-managed caching mechanism. It encompasses the fundamental constraints upon components, connectors, and data that define the basis of the Web architecture, and thus the essence of its behavior as a network-based application. REST distinguishes three classes of architectural elements, they are:

  • Connectors :Connectors represent the activities involved in accessing resources and transferring representations.

  • Components :In REST, the various software that interacts with one another are called components

  • Data Elements: The 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.


Request Methods:

The following HTTP request methods can be used

  • GET : This method should be used to get data and should give same results in multiple calls.

  • POST: Should create a new resource by ideally returning JSON with link to newly created resource.

  • PUT: It is used to update a known resource.

  • DELETE: Used to delete a resource.


Richardson Maturity Model

Richardson Maturity Model is used to identify the maturity level of a Restful Web Service. Following are the different levels and their characteristics:

Level 0 : Expose SOAP web services in REST style. Expose action based services (http://server/getPosts, http://server/deletePosts, http://server/doThis, http://server/doThat etc) using REST.

Level 1 : Expose Resources with proper URI’s (using nouns). Ex: http://server/employees

Level 2 : Resources use proper URI’s + HTTP Methods. Example, to update an account, you do a PUT to . The create an account, you do a POST to .

Level 3 : HATEOAS (Hypermedia as the engine of application state). You will tell not only about the information being requested but also about the next possible actions that the service consumer can do.


Implementation

These days SpringBoot is extensively used to develop the REST APIs. Spring Boot is built on top of spring framework. It makes building code much easier with minimizing manual configuration.


As RESTful Web Services work with HTTP URL Paths.

Following are the best practices to be adhered to while designing a RESTful Web Service −

  • Validation − Validate all inputs on the server. Protect your server against SQL injection attacks.

  • Session Based Authentication − Use session based authentication to authenticate a user whenever a request is made to a Web Service method.

  • No Sensitive Data in the URL − Never use username, password or session token in a URL, these values should be passed to Web Service via the POST method.

  • Restriction on Method Execution − Allow restricted use of methods like GET, POST and DELETE methods.

  • Validate Malformed XML/JSON − Check for well-formed input passed to a web service method.

Response Codes Etiquettes:

When you implement an operation, make sure you return the correct response status.

For example, when a particular resource is not found, don’t throw a server exception, Instead, send out the appropriate response code in the response message, such as 404.

When there is actually a server exception, send back a 500 code. When there is a validation error, send the code for a bad request.


Annotations:

JAX-RS API uses annotations. The most common annotations are

@Path: Defines the path used to access the web service

@PathParam:Injects values from the URL into a method parameter

@FormParam:Injects values from an HTML form into a method parameter

@Consumes:Specifies the type of the request data

@Produces:Specifies the type of the response



Conclusion:


This blog explained about the technicality of the REST webservices in JAVA.

























11 views1 comment

1 Comment

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Guest
Mar 16, 2023
Rated 5 out of 5 stars.

Nice article

Like
bottom of page