How To Handle Delete Request in Rest Webservice

The specialty of REST is it uses HTTP methods for transaction.  For example it supports GET, POST, PUT and DELETE methods.  We have seen many examples of GET and also also some examples of POST method.  In this example we will see how to use DELETE method.  DELETE method is used to delete a resource in REST services.

For example assume that there is a resource such as http://localhost:8080/RestWebProject/rest/book/1 which represents the book with book id 1. When this resource is invoked with GET method then the REST service should return the book object with the id 1.  If this same method is invoked with the DELETE method then the resource which is Book with id 1 should be removed from the server.

Let us see a simple example where we will use GET and DELETE methods.

DeleteService.java

This class has two methods.  One for GET method and the other one for DELETE method.  They are denoted by the annotations.  The logic is very simple.  Both methods get list of books from a ArrayList. If matching book id is found it is deleted for DELETE method and for the GET method the data is returned.

package com.freetipscentral;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
 
import com.freetipscentral.domain.Book;
 
@Path("/book")
public class DeleteService {
 
	@DELETE
	@Produces(MediaType.TEXT_PLAIN)
	@Path("{bookId}")
	public String deleteBook(@Context HttpHeaders httpHeaders, @PathParam("bookId") int bookId) {
		List<Book> books = getBooks();
 
		for (Book book : books) {
			if (book.getSerialNumber() == bookId)
				books.remove(book);
		}
 
		return "Items remaining : "+books.size();
	}
 
	@GET
	@Produces(MediaType.APPLICATION_XML)
	@Path("{bookId}")
	public Response getBook(@Context HttpHeaders httpHeaders, @PathParam("bookId") int bookId) {
		List<Book> books = getBooks();
 
		for (Book book : books) {
			if (book.getSerialNumber() == bookId) {
				return Response.ok(book).build();
			}
		}
 
		return Response.status(Status.NOT_FOUND).build();
	}
 
	private List<Book> getBooks() {
		List<Book> books = new ArrayList<Book>();
		Book book1 = new Book();
		book1.setName("Head First Java");
		book1.setSerialNumber(1);
 
		Book book2 = new Book();
		book2.setName("Clean Coders");
		book2.setSerialNumber(2);
 
		books.add(book1);
		books.add(book2);
 
		return books;
	}
}

 

Book.java

package com.freetipscentral.domain;
 
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class Book {
	String name;
	int serialNumber;
 
	public Book() {}
 
	public Book(String name, int serialNumber) {
		this.name = name;
		this.serialNumber = serialNumber;
	}
 
	public String getName() {
		return name;
	}
 
	public void setName(String name) {
		this.name = name;
	}
 
	public int getSerialNumber() {
		return serialNumber;
	}
 
	public void setSerialNumber(int serialNumber) {
		this.serialNumber = serialNumber;
	}
 
	public String toString() {
		return serialNumber + " "  + name;
	}	
}

 

Here are the output – with DELETE method

REST service DELETE Method

 

Here are the output – with GET method

REST GET Request

One thought to “How To Handle Delete Request in Rest Webservice”

Comments are closed.