How To Write a Java Client for PUT Method Rest Service

PUT method is used in REST service to update data. Usually you will use the PUT method to send message to REST service.  If matching id is found the data on the server will be updated.  Let us see an example of how to call such a REST service and update the data on the server.

Service Class

PutJsonService.java

package com.freetipscentral;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

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

@Path("/putJSON")
public class PutJsonService {

  @Produces(MediaType.APPLICATION_JSON)
  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  public List<Book> getBookAsJSON(Book book) {
    List<Book> books = BookCreator.getBooks();
    for(Book eachBook : books)  {
      if(book.getSerialNumber() == eachBook.getSerialNumber()) {
        eachBook.setName(book.getName());
      }
    }
    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 PUT client

JerseyRestPutClient.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 JerseyRestPutClient {
  public static void main(String args[]) {
    Client client = Client.create();
    WebResource resource = client.resource("http://localhost:8080/RestWebService/rest/putJSON");
    Book book = new Book("Deep Work",1);
    List<Book> response =resource.type(MediaType.APPLICATION_JSON)
    .accept(MediaType.APPLICATION_JSON)
    .put(new GenericType<List<Book>>(){},book);
    
    for(Book eachBook : response){
      System.out.println(eachBook);
    }
  }
}

Here is the Output

As you can see, the book 1 which was originally Head First Java was changed to Deep Work.

1 Deep Work
2 Head First Design Pattern

Video Version of Tutorial

Here is the video version of this tutorial I have created.

One thought to “How To Write a Java Client for PUT Method Rest Service”

Comments are closed.