How To Post a JSON Object to Rest Webservice

Till now we saw how to use GET requests to receive data from the REST webservice.  In this tutorial we will learn how to post a data to a REST service.

REST uses HTTP operations for the transactions as it need HTTP for communication between client and server.  As per REST principle GET operation is used for retrieving data and POST operation is for adding or posting data.  In this example we will assume that there is a item list which contains books and their quantity.  Now we want to add a new book in the item list.  The book detail will be sent from the rest client in JSON format and it will be added to the list.

RestPostDataExample

  • The method getJSONBook is annotated with @POST.  That means that it will only accept POST operation.  To reach this method POST operation need to be sent from the client.
  • It is also annotated with @Consumes(MediaType.APPLICATION_JSON) which means that this method expects a JSON body in the request.  So the rest client must add the JSON in the request.
  • Next question is what will the JSON request contain?  To answer this look at the parameter of the method public Order getJSONBook(Book book).  getJSONBook contains Book as the parameter.  That means that JSON object in the method must be such that it can represent a Book object.
  • Once the method receives the JSON object it will map it to a Book object and it will be available in the book variable.  Then the book will be added to the existing item list and returned to the client in JSON format.
package com.freetipscentral;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
 
import com.freetipscentral.domain.Book;
import com.freetipscentral.domain.Item;
import com.freetipscentral.domain.Order;
 
@Path("/saveBookList")
public class RestPostDataExample {
 
 
	@POST
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Order getJSONBook(Book book) {
		List<Item> items = getItems();
 
		Item item = new Item();
		item.setBook(book);
		item.setQuantity(1);
 
		items.add(item);
 
		Order order = new Order();
		order.setItems(items);
 
		return order;
	}
 
 
	private List<Item> getItems() {
		Book book1 = new Book("Harry Potter",1);
		Book book2 = new Book("Guliver Travels",2);
 
		Item item1 = new Item();
		item1.setBook(book1);
		item1.setQuantity(2);
 
		Item item2 = new Item();
		item2.setBook(book2);
		item2.setQuantity(4);
 
		List<Item> items = new ArrayList<Item>();
		items.add(item1);
		items.add(item2);
		return items;
	}
}

 

Here are the other classes.

Order class contains list of items.  Each Item contains Book object and order quantity.

Order.java

package com.freetipscentral.domain;
 
import java.util.List;
 
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
public class Order {
	List<Item> items;
 
	public List<Item> getItems() {
		return items;
	}
 
	public void setItems(List<Item> items) {
		this.items = items;
	}
}

Item.java

package com.freetipscentral.domain;
 
public class Item {
	Book book;
	int quantity;
 
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
}

Book.java

package com.freetipscentral.domain;
 
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 is how to access the service

http://localhost:8080/RestWebService/rest/saveBookList

JSON Body in the request 

{
 "serialNumber":3,
 "name":"Clean Coder"
}

Here is the output

{
 "items": [
 {
 "book": {
 "name": "Harry Potter",
 "serialNumber": 1
 },
 "quantity": 2
 },
 {
 "book": {
 "name": "Guliver Travels",
 "serialNumber": 2
 },
 "quantity": 4
 },
 {
 "book": {
 "name": "Clean Coder",
 "serialNumber": 3
 },
 "quantity": 1
 }
 ]
}