How To Write a Java Client for DELETE Method Rest Service

DELETE method is used in REST service to delete data on the server. When deleting the right way to do it is using the DELETE method to send message to REST service.  If matching id is found the data on the server will be deleted.  Let us see an example of how to call such a REST service and delete the data on the server.

Service Class

DeleteJsonService.java

In this class the method maps to DELETE method.  When the request comes to the service, the client send a book id as request parameter.  The service then iterates the Books in the system and if it finds the id it deletes the Book and sends back the remaining books to the client.  The input parameter is captured in the @PathParam(“bookId”) int bookId and then it is matched with the books  in the system.

package com.freetipscentral;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.freetipscentral.domain.Book;
import com.freetipscentral.utility.BookCreator;

@Path("/deleteJSON")
public class DeleteJsonService {

  @Produces(MediaType.APPLICATION_JSON)
  @DELETE
  @Consumes(MediaType.APPLICATION_JSON)
  @Path("/{bookId}")
  public List<Book> deleteBook(@PathParam("bookId") int bookId ) {
    List<Book> books = BookCreator.getBooks();
    for(Book eachBook : books)  {
      if(eachBook.getSerialNumber() == bookId) {
        books.remove(eachBook);
      }
    }
    return books;
  }
}

Domain Class

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;
  }	
}

Utility Class To Create Book List on Server

BookCreator.java

This class has a static method to create two book objects and return the list.

package com.freetipscentral.utility;
 
import java.util.ArrayList;
import java.util.List;
 
import com.freetipscentral.domain.Book;
 
public class BookCreator {
 
  public static List<Book> getBooks() {
    List<Book> books = new ArrayList<Book>();
    Book book1 = new Book("Head First Java",1);
    Book book2 = new Book("Head First Design Pattern",2);
    books.add(book1);
    books.add(book2);
    return books;
  }
}

Here is the DELETE client

JerseyRestDeleteClient.java

package com.freetipscentral;

import java.util.List;

import javax.ws.rs.core.MediaType;

import com.freetipscentral.domain.Book;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;

public class JerseyRestDeleteClient {
  public static void main(String args[]) {
    Client client = Client.create();
    WebResource resource = client.resource("http://localhost:8080/RestWebService/rest/deleteJSON/1");
    List<Book> response =resource.type(MediaType.APPLICATION_JSON)
    .accept(MediaType.APPLICATION_JSON)
    .delete(new GenericType<List<Book>>(){});
    
    for(Book eachBook : response){
      System.out.println(eachBook);
    }
  }
}

Here is the Output

As you can see, the id for the book 1 matched the input book id hence it was deleted.  Rest of the books were returned.

2 Head First Design Pattern

 

Video Version of this Tutorial

I have also create a video tutorial and you can find it here –