Jersey Client To Get JSON Response From POST Method

In one of the previous tutorial how to post JSON list to REST service as arrayList we saw how to handle POST request which submitted JSON data and returned list JSON as output from the service.  To test the service we used a chrome based REST client called Postman.  In this article we will see how to write a Java client based on Jersey API to post request to the service and receive the response back from the service.

Here is the Service Class

PostJsonService.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("/postJSON")
public class PostJsonService {
 
	@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;
	}
}

Here are the domain classes.

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

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

Here is a Helper Class

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

Finally here is the Java client for the Rest Service

I have used Jersey 1.9

RestClient.java

In the client class 3 book objects are created and posted to the REST service.  The Rest service adds the 3 books to its pre-existing list of 2 books and then return the 5 books to the client.  The client then prints the books in console.

package com.freetipscentral.client;
 
import java.util.ArrayList;
import java.util.List;
 
import javax.ws.rs.core.MediaType;
 
import com.freetipscentral.domain.Book;
import com.freetipscentral.domain.Books;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;
 
public class RestClient {
	public static void main(String args[]) {
 
		Books books = new Books();
 
		Book book1 = new Book("Head First Java",1);
		Book book2 = new Book("Head First Design Pattern",2);
		Book book3 = new Book("My Experiments With Truth",3);
 
		List<Book> bookList = new ArrayList<Book>();
		bookList.add(book1);
		bookList.add(book2);
		bookList.add(book3);
		books.setBooks(bookList);
		books.setTotalPrice(100);
 
		Client client = Client.create();
		WebResource webResource = client.resource("http://localhost:8080/RestWebService/rest/postJSON");
 
		List<Book> booksFromRest = webResource.type(MediaType.APPLICATION_JSON)
				      .accept(MediaType.APPLICATION_JSON)
				      .post(new GenericType<List<Book>>() {}, books);
		for(Book eachBook : booksFromRest) {
			System.out.println(eachBook);
		}
	}
}

The jar files required for the client are as follows

  • genson-1.3.jar
  • jersey-bundle-1.9.jar
  • jersey-core-1.8.jar

Here is the Output

1 Head First Java
2 Head First Design Pattern
1 Head First Java
2 Head First Design Pattern
3 My Experiments With Truth

One thought to “Jersey Client To Get JSON Response From POST Method”

Comments are closed.