REST Web Services

REST Web Services

REST (Representational State Transfer) web services are a set of web services that adhere to REST architectural principles. REST is an architectural style, not a protocol, that leverages HTTP methods to create, read, update, and delete resources (often referred to by their URIs). RESTful web services are stateless, meaning each request from a client to the server must contain all the information needed to understand and process the request.

REST Web Services

Key Principles of REST:

  • 1.  Client-Server Architecture : Separation of concerns between client and server.
  • 2.  Statelessness : Each request from the client must contain all the information needed by the server to fulfill the request.
  • 3.  Cacheability : Responses must define themselves as cacheable or not to improve efficiency.
  • 4.  Layered System : Architecture can be composed of multiple layers, improving scalability and security.
  • 5.  Uniform Interface : Simplifies and decouples the architecture, allowing each part to evolve independently.

HTTP Methods in REST:

  • GET : Retrieve data from the server.
  • POST : Send data to the server to create a new resource.
  • PUT : Update an existing resource.
  • DELETE : Remove a resource from the server.

Explanation in Java Example

To illustrate how to create a RESTful web service in Java, we will use JAX-RS (Java API for RESTful Web Services). JAX-RS provides a set of annotations to create RESTful web services easily.

1. Create a Simple RESTful Web Service
xml
<dependencies>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.1.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.33</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.inject</groupId>
        <artifactId>jersey-hk2</artifactId>
        <version>2.33</version>
    </dependency>
</dependencies>

Service Class
java
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class HelloWorldService {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello() {
        return "Hello, World!";
    }
}

Application Config Class
java
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("/api")
public class RestApplication extends Application {
    // No need to implement any methods
}

web.xml Configuration (for a servlet container like Tomcat)
xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <display-name>RestApp</display-name>

    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.example.RestApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/api/*</url-pattern>
    </servlet-mapping>

</web-app>

Project Structure
src
 └── main
     ├── java
     │   └── com
     │       └── example
     │           ├── HelloWorldService.java
     │           └── RestApplication.java
     └── webapp
         └── WEB-INF
             └── web.xml

2.  Run the Application

Deploy the project in a servlet container like Apache Tomcat. Once deployed, you can access the RESTful web service at http://localhost:8080/your-context-root/api/hello.

When you navigate to the URL, you should see the text “Hello, World!” returned by the service.

Explanation

  • 1.  Service Class :
    • The @Path(“/hello”) annotation defines the URI path for accessing this service.
    • The @GET annotation specifies that this method will respond to HTTP GET requests.
    • The @Produces(MediaType.TEXT_PLAIN) annotation indicates that the response will be plain text.
  • 2.  Application Config Class :
    • The @ApplicationPath(“/api”) annotation sets the base URI for all resource URIs provided by HelloWorldService.
  • 3.  web.xml Configuration :
    • Defines the servlet configuration for Jersey (the JAX-RS implementation).
    • Maps the Jersey servlet to the /api/* URL pattern.