Java Client To Get Xml Or JSON List From Get Method in Rest Service

We saw previously how to get XML data using a a Java Client from a Rest service.  In this post we will see how to get List of objects from Xml and Json from a Rest service in a Java client.

We will basically use the same structure as the previous post but the with a little difference to handle List.  Here are the classes.

Here is the Domain class.

Book.class

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

 

To get JSON response here are the Service and Client Classes.

Service class.

RestXMLService.java

package com.freetipscentral;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.freetipscentral.domain.Book;

@Path("/getXML")
public class RestXMLService {

  @Produces(MediaType.APPLICATION_JSON)
  @GET
  public List<Book> getBookAsXML() {
    
    Book book1 = new Book("Head First Java", 1);
    Book book2 = new Book("Head First Design Patterns", 2);
    
    List<Book> bookList = new ArrayList<Book>();
    bookList.add(book1);
    bookList.add(book2);
    return bookList;
  }
}

Client for JSON

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

To get XML response here are the Service and Client Classes.

Service class.

RestXMLService.java

package com.freetipscentral;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.freetipscentral.domain.Book;

@Path("/getXML")
public class RestXMLService {

  @Produces(MediaType.APPLICATION_XML)
  @GET
  public List<Book> getBookAsXML() {
    
    Book book1 = new Book("Head First Java", 1);
    Book book2 = new Book("Head First Design Patterns", 2);
    
    List<Book> bookList = new ArrayList<Book>();
    bookList.add(book1);
    bookList.add(book2);
    return bookList;
  }
}

Client for XML

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 JerseyRestGettClient {
  public static void main(String args[]) {
    Client client = Client.create();
    WebResource resource = client.resource("http://localhost:8080/RestWebService/rest/getXML");
    
    List<Book> response =resource
    .accept(MediaType.APPLICATION_XML)
    .get(new GenericType<List<Book>>(){});
    
    for(Book book : response){
      System.out.println(book);
    }
  }
}

Here are the jar files needed

The jar files required 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 Patterns

Here is the video version