title: Apache HttpClient - REST API: Issue in converting response to customized object which is put as SerializableEntity
body: <p>I am using Apache HttpClient to put/get customized object using REST APIs. Below is the sample code. My putObject() method works fine and I could serialize Person object and put properly. However, while getting the object, I got below error:</p>
  
  <pre><code>Exception in thread "main" java.lang.ClassCastException: [B cannot be cast to Person    at MyTest.demoGetRESTAPI(MyTest.java:88)    at MyTest.main(MyTest.java:21)
  </code></pre>
  
  <p>Seems the code to build Person object out of response entity is not correct </p>
  
  <pre><code>  HttpEntity httpEntity = response.getEntity();
              byte[] resultByteArray = EntityUtils.toByteArray(httpEntity);
              Person person = (Person)SerializationUtils.deserialize(resultByteArray);
  </code></pre>
  
  <p>Am I doing somthing wrong while getting byte[] array and converting to Person object. Please help me out to solve this issue.</p>
  
  <p>Complete Example Program:</p>
  
  <pre><code>import java.io.Serializable;
  
  import org.apache.commons.lang.SerializationUtils;
  import org.apache.http.HttpEntity;
  import org.apache.http.HttpResponse;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.client.methods.HttpPut;
  import org.apache.http.entity.SerializableEntity;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.apache.http.util.EntityUtils;
  
  public class MyTest {
  
      public static void main(String[] args) throws Exception  {
  
          putObject();
  
          getObject();
      }
  
      public static void putObject() throws Exception
      {
          HttpClient httpClient = new DefaultHttpClient();
  
         Person person = new Person();
         person.setName("Narendra");
         person.setId("1");
  
          try
          {
              //Define a postRequest request
              HttpPut putRequest = new HttpPut("http://localhost:9084/ehcache-server/rest/screeningInstance/2221");
  
              //Set the API media type in http content-type header
              putRequest.addHeader("content-type", "application/x-java-serialized-object");
  
              //Set the request put body
              SerializableEntity personSEntity = new SerializableEntity(SerializationUtils.serialize(person));
              putRequest.setEntity(personSEntity);
  
              //Send the request; It will immediately return the response in HttpResponse object if any
              HttpResponse response = httpClient.execute(putRequest);
  
              //verify the valid error code first
              int statusCode = response.getStatusLine().getStatusCode();
              if (statusCode != 201)
              {
                  throw new RuntimeException("Failed with HTTP error code : " + statusCode);
              }
          }
          finally
          {
              //Important: Close the connect
              httpClient.getConnectionManager().shutdown();
          }
      }
  
      public static void getObject() throws Exception
      {
          DefaultHttpClient httpClient = new DefaultHttpClient();
          try
          {
              //Define a HttpGet request; You can choose between HttpPost, HttpDelete or HttpPut also.
              //Choice depends on type of method you will be invoking.
              HttpGet getRequest = new HttpGet("http://localhost:9084/ehcache-server/rest/screeningInstance/2221");
  
              //Set the API media type in http accept header
              getRequest.addHeader("accept", "application/x-java-serialized-object");
  
              //Send the request; It will immediately return the response in HttpResponse object
              HttpResponse response = httpClient.execute(getRequest);
  
              //verify the valid error code first
              int statusCode = response.getStatusLine().getStatusCode();
              if (statusCode != 200)
              {
                  throw new RuntimeException("Failed with HTTP error code : " + statusCode);
              }
  
              //Now pull back the response object
  
              HttpEntity httpEntity = response.getEntity();
              byte[] resultByteArray = EntityUtils.toByteArray(httpEntity);
              Person person = (Person)SerializationUtils.deserialize(resultByteArray);           
          }
          finally
          {
              //Important: Close the connect
              httpClient.getConnectionManager().shutdown();
          }
      }
  
  }
  
  class Person implements Serializable{
      String name;
      String id;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public String getId() {
          return id;
      }
  
      public void setId(String id) {
          this.id = id;
      }
  
      @Override
      public String toString() {
          return "Person [name=" + name + ", id=" + id + "]";
      }
  
  
  }
  </code></pre>