How To Post JSON List To REST Service As ArrayList

In the previous tutorial we saw how to POST XML List to a Jersey REST web service as ArrayList.  We sent a simple xml structure which represented a book.  What about sending a JSON list instead of XML list?

In this tutorial we will learn how to send a list of books in JSON format to the REST service using POST method.  The JSON List will be contained in a structure which will map to Books class as you can see below.

The service should accept the books sent and add them to the book list available in the system.  The service will then return back a list which contains pre-existing books in the system along with the books sent to the service.

Here is How the Service works

The REST service takes a list of Book objects and their price in a Books object in JSON format using POST method.  Although the totalPrice of books is sent it is mainly for demonstration purpose.  The price is not processed.  It is sent just to show that we can send other elements along with book list.

The service is annotated with @POST so that POST method can be used from the client.  This service takes JSON data as input and it gets converted to Books object which has structure to match the JSON input.  The Books class is a container class which contains list of book objects and a price field.

Service then retrieves a list of books from a utility class and adds the books sent to the method.  Then the list is sent back to the client.

To create a list of books a utility method has been created in the BookCreator class.  That class simply returns a list which contains two Book objects.

To receive the book as JSON two things are needed.

The input JSON should be mapping to the structure of the Books class.  You will see the example below.  The Content-Type should be application/json.

 

PostXmlService.java

package com.freetipscentral;
 
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.Books;
import com.freetipscentral.utility.BookCreator;
 
@Path("/postXML")
public class PostXmlService {
 
	@Produces(MediaType.APPLICATION_JSON)
	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	public List<Book> getBookAsJSON(Books books) {
		List<Book> bookList = BookCreator.getBooks();
		bookList.addAll(books.getBooks());
 
		return bookList;
	}
}

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

Books.java

[codesyntax lang=”java”]

package com.freetipscentral.domain;

import java.util.List;

public class Books {
	
	List<Book> books;
	int totalPrice;
	
	public List<Book> getBooks() {
		return books;
	}
	public void setBooks(List<Book> books) {
		this.books = books;
	}
	public int getTotalPrice() {
		return totalPrice;
	}
	public void setTotalPrice(int totalPrice) {
		this.totalPrice = totalPrice;
	}
		
}

[/codesyntax]

BookCreator.java

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 input Body

{
 
 "books": [
  {
    "name": "Head First Java",
    "serialNumber": 1
  },
  {
    "name": "Head First Design Pattern",
    "serialNumber": 2
  },
  {
    "name": "My Experiments With Truth",
    "serialNumber": 3
  }],
  "totalPrice" :100
}

Input Header

Content-Type  application/json

Here is the Output

[
 {
 "name": "Head First Java",
 "serialNumber": 1
 },
 {
 "name": "Head First Design Pattern",
 "serialNumber": 2
 },
 {
 "name": "Head First Java",
 "serialNumber": 1
 },
 {
 "name": "Head First Design Pattern",
 "serialNumber": 2
 },
 {
 "name": "My Experiments With Truth",
 "serialNumber": 3
 }
]

Here is the Video version of this tutorial