title: Getting JSON out put from restful java client
body: <p>I'm developing a web service using REST (Jersey 1.8). Currently I'm using XML to communicate between the Java client and the server.</p>
  
  <p>I need to change it to JSON: how can I do that? I have bunch of auto generated code from NetBeans, and have no idea what to do and how. When the testing the service it shows the JSON data. What I'm unable to do is deal with it within my <code>main</code> method.
  <img src="https://i.stack.imgur.com/EWuBZ.png" alt="enter image description here"></p>
  
  <p>these are the tutorial I followed</p>
  
  <ul>
  <li><a href="http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices/RESTfulWebservices.htm" rel="nofollow noreferrer">http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices/RESTfulWebservices.htm</a></li>
  <li><a href="http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices_Part2/RESTfulWebservicesPart2.htm" rel="nofollow noreferrer">http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices_Part2/RESTfulWebservicesPart2.htm</a></li>
  <li><a href="http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices_Part3/RESTfulWebservicesPart3.htm" rel="nofollow noreferrer">http://www.oracle.com/webfolder/technetwork/tutorials/obe/java/RESTfulWebServices_Part3/RESTfulWebservicesPart3.htm</a></li>
  </ul>
  
  <p>My Java client <code>main</code> method:</p>
  
  <pre><code>public class SOATestClient {
  
      /**
       * @param args the command line arguments
       */
      public static void main(String[] args) {
          PersonJerseyClient client = new PersonJerseyClient();
          ClientResponse response = client.findAll_XML(ClientResponse.class);
  
  
          GenericType&lt;List&lt;Person&gt;&gt; genericType = new GenericType&lt;List&lt;Person&gt;&gt;() {
          };
  // Returns an ArrayList of Players from the web service
          List&lt;Person&gt; data = new ArrayList&lt;Person&gt;();
          data = (response.getEntity(genericType));
          System.out.println("Retreiving and Displaying Players Details");
          for (Person person : data) {
              System.out.println("FirstName: " + person.getName());
              System.out.println("ID : " + person.getId());
              System.out.println(" Age : " + person.getAge());
          }
          client.close();
      }
  }
  </code></pre>
  
  <p>personjerseycilent</p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package jerseyclients;
  
  import com.sun.jersey.api.client.Client;
  import com.sun.jersey.api.client.UniformInterfaceException;
  import com.sun.jersey.api.client.WebResource;
  
  /**
   * Jersey REST client generated for REST resource:PersonFacadeREST
   * [entity.person]&lt;br&gt;
   *  USAGE:
   * &lt;pre&gt;
   *        PersonJerseyClient client = new PersonJerseyClient();
   *        Object response = client.XXX(...);
   *        // do whatever with response
   *        client.close();
   * &lt;/pre&gt;
   *
   * @author rj45
   */
  public class PersonJerseyClient {
      private WebResource webResource;
      private Client client;
      private static final String BASE_URI = "http://localhost:8080/SOATestService/resources";
  
      public PersonJerseyClient() {
          com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
          client = Client.create(config);
          webResource = client.resource(BASE_URI).path("entity.person");
      }
  
      public void remove(String id) throws UniformInterfaceException {
          webResource.path(java.text.MessageFormat.format("{0}", new Object[]{id})).delete();
      }
  
      public String countREST() throws UniformInterfaceException {
          WebResource resource = webResource;
          resource = resource.path("count");
          return resource.accept(javax.ws.rs.core.MediaType.TEXT_PLAIN).get(String.class);
      }
  
      public &lt;T&gt; T findAll_XML(Class&lt;T&gt; responseType) throws UniformInterfaceException {
          WebResource resource = webResource;
          return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
      }
  
      public &lt;T&gt; T findAll_JSON(Class&lt;T&gt; responseType) throws UniformInterfaceException {
          WebResource resource = webResource;
          return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
      }
  
      public void edit_XML(Object requestEntity) throws UniformInterfaceException {
          webResource.type(javax.ws.rs.core.MediaType.APPLICATION_XML).put(requestEntity);
      }
  
      public void edit_JSON(Object requestEntity) throws UniformInterfaceException {
          webResource.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).put(requestEntity);
      }
  
      public void create_XML(Object requestEntity) throws UniformInterfaceException {
          webResource.type(javax.ws.rs.core.MediaType.APPLICATION_XML).post(requestEntity);
      }
  
      public void create_JSON(Object requestEntity) throws UniformInterfaceException {
          webResource.type(javax.ws.rs.core.MediaType.APPLICATION_JSON).post(requestEntity);
      }
  
      public &lt;T&gt; T findRange_XML(Class&lt;T&gt; responseType, String from, String to) throws UniformInterfaceException {
          WebResource resource = webResource;
          resource = resource.path(java.text.MessageFormat.format("{0}/{1}", new Object[]{from, to}));
          return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
      }
  
      public &lt;T&gt; T findRange_JSON(Class&lt;T&gt; responseType, String from, String to) throws UniformInterfaceException {
          WebResource resource = webResource;
          resource = resource.path(java.text.MessageFormat.format("{0}/{1}", new Object[]{from, to}));
          return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
      }
  
      public &lt;T&gt; T find_XML(Class&lt;T&gt; responseType, String id) throws UniformInterfaceException {
          WebResource resource = webResource;
          resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
          return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_XML).get(responseType);
      }
  
      public &lt;T&gt; T find_JSON(Class&lt;T&gt; responseType, String id) throws UniformInterfaceException {
          WebResource resource = webResource;
          resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{id}));
          return resource.accept(javax.ws.rs.core.MediaType.APPLICATION_JSON).get(responseType);
      }
  
      public void close() {
          client.destroy();
      }
  
  }
  </code></pre>
  
  <p>I try to access it with the following, and deal it with same way as XML,</p>
  
  <pre><code>ClientResponse response = client.findAll_JSON(ClientResponse.class);
  </code></pre>
  
  <p>but it gives me </p>
  
  <pre><code>Exception in thread "main" javax.ws.rs.WebApplicationException: javax.xml.bind.UnmarshalException
   - with linked exception:
  [com.sun.istack.internal.SAXParseException2; lineNumber: 0; columnNumber: 0; unexpected element (uri:"", local:"id"). Expected elements are &lt;{}person&gt;]
      at com.sun.jersey.core.provider.jaxb.AbstractListElementProvider.readFrom(AbstractListElementProvider.java:251)
      at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:553)
      at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:523)
      at soatestclient.SOATestClient.main(SOATestClient.java:33)
  Caused by: javax.xml.bind.UnmarshalException
  </code></pre>
  
  <p>I would be grateful to if you could help me on this matter. Thank you!</p>