Commonly Used HTTP Methods
In RESTful web services, HTTP methods (also known as HTTP verbs) are used to perform different operations on resources. These methods define the actions that clients can perform on resources identified by URIs (Uniform Resource Identifiers). Each HTTP method has a specific purpose and meaning, contributing to the stateless and resource-oriented nature of REST architecture.
Table of Contents
Commonly Used HTTP Methods:
- 1. Â GET : Retrieves data from the server. It should not modify any data on the server and should be idempotent (making the same request multiple times should have the same effect as making it once).
- 2. Â POST : Submits data to the server to create a new resource. It can also be used to update an existing resource if the server allows it, though typically, POST is used for creating new resources.
- 3. Â PUT : Updates an existing resource or creates a new resource if it does not exist at the specified URI. Unlike POST, PUT is idempotent, meaning multiple identical requests should have the same effect as a single request.
- 4. Â DELETE : Removes a resource identified by a URI from the server. It is idempotent, meaning that making the same DELETE request multiple times should have the same effect as making it once.
- 5. Â PATCH : Applies partial modifications to a resource. It is used to apply a set of changes to an existing resource. PATCH requests are typically used when you want to apply a partial update to a resource rather than replacing it entirely (as with PUT).
Explanation in Java Example
To demonstrate the use of these HTTP methods in a Java-based RESTful service, let’s extend the previous example of a Book resource to support all these operations.
- 1. Â Dependencies (Maven pom.xml) : Already provided in previous examples.
- 2. Â Book Resource Class (BookResource.java) :
java
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;
@Path("/books")
public class BookResource {
private static List<Book> books = new ArrayList<>();
// GET all books
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Book> getAllBooks() {
return books;
}
// GET book by ID
@GET
@Path("/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response getBookById(@PathParam("id") int id) {
if (id >= 0 && id < books.size()) {
return Response.ok(books.get(id)).build();
} else {
return Response.status(Response.Status.NOT_FOUND).entity("Book not found").build();
}
}
// POST create a new book
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response createBook(Book book) {
books.add(book);
return Response.status(Response.Status.CREATED).build();
}
// PUT update an existing book
@PUT
@Path("/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public Response updateBook(@PathParam("id") int id, Book book) {
if (id >= 0 && id < books.size()) {
books.set(id, book);
return Response.ok().build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
// DELETE delete a book by ID
@DELETE
@Path("/{id}")
public Response deleteBook(@PathParam("id") int id) {
if (id >= 0 && id < books.size()) {
books.remove(id);
return Response.ok().build();
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
}
}
- 3. Â Book Class (Book.java) : Already provided in previous examples.
- 4. Â Application Config Class (RestApplication.java) : Already provided in previous examples.
- 5. Â web.xml Configuration : Already provided in previous examples.
Usage Examples:
- GET all books : Retrieve all books
GET http://localhost:8080/your-context-root/api/books
- GET book by ID : Retrieve a specific book by ID
GET http://localhost:8080/your-context-root/api/books/{id}
- POST create a new book : Create a new book
POST http://localhost:8080/your-context-root/api/books
Body: { “id”: 1, “title”: “Java Programming”, “author”: “John Doe” }
- PUT update an existing book : Update an existing book by ID
PUT http://localhost:8080/your-context-root/api/books/{id}
Body: { “id”: 1, “title”: “Updated Java Programming”, “author”: “Jane Doe” }
- DELETE delete a book by ID : Delete a book by ID
DELETE http://localhost:8080/your-context-root/api/books/{id}
Explanation
- GET : Used to retrieve resources. In the example, getAllBooks() retrieves all books, and getBookById(int id) retrieves a specific book by its ID.
- POST : Used to create new resources. createBook(Book book) adds a new book to the collection.
- PUT : Used to update existing resources. updateBook(int id, Book book) updates the book with the specified ID.
- DELETE : Used to delete resources. deleteBook(int id) removes the book with the specified ID from the collection.
Each method corresponds to a specific CRUD operation (Create, Read, Update, Delete) and adheres to REST principles by using appropriate HTTP methods and URI paths.