Apache HttpClient - REST API: Issue in converting response to customized object which is put as SerializableEntity -
i using apache httpclient put/get customized object using rest apis. below sample code. putobject() method works fine , serialize person object , put properly. however, while getting object, got below error:
exception in thread "main" java.lang.classcastexception: [b cannot cast person @ mytest.demogetrestapi(mytest.java:88) @ mytest.main(mytest.java:21)
seems code build person object out of response entity not correct
httpentity httpentity = response.getentity(); byte[] resultbytearray = entityutils.tobytearray(httpentity); person person = (person)serializationutils.deserialize(resultbytearray);
am doing somthing wrong while getting byte[] array , converting person object. please me out solve issue.
complete example program:
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 postrequest request httpput putrequest = new httpput("http://localhost:9084/ehcache-server/rest/screeninginstance/2221"); //set api media type in http content-type header putrequest.addheader("content-type", "application/x-java-serialized-object"); //set request put body serializableentity personsentity = new serializableentity(serializationutils.serialize(person)); putrequest.setentity(personsentity); //send request; return response in httpresponse object if httpresponse response = httpclient.execute(putrequest); //verify valid error code first int statuscode = response.getstatusline().getstatuscode(); if (statuscode != 201) { throw new runtimeexception("failed http error code : " + statuscode); } } { //important: close connect httpclient.getconnectionmanager().shutdown(); } } public static void getobject() throws exception { defaulthttpclient httpclient = new defaulthttpclient(); try { //define httpget request; can choose between httppost, httpdelete or httpput also. //choice depends on type of method invoking. httpget getrequest = new httpget("http://localhost:9084/ehcache-server/rest/screeninginstance/2221"); //set api media type in http accept header getrequest.addheader("accept", "application/x-java-serialized-object"); //send request; return response in httpresponse object httpresponse response = httpclient.execute(getrequest); //verify valid error code first int statuscode = response.getstatusline().getstatuscode(); if (statuscode != 200) { throw new runtimeexception("failed http error code : " + statuscode); } //now pull response object httpentity httpentity = response.getentity(); byte[] resultbytearray = entityutils.tobytearray(httpentity); person person = (person)serializationutils.deserialize(resultbytearray); } { //important: close 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 + "]"; } }
i got solution. mistake in code:
while putting object, have written below code. doing 2 time serialization. first person object byte[] , second byte[] byte[].
serializableentity personsentity = new serializableentity(serializationutils.serialize(person)); putrequest.setentity(personsentity);
this right approach:
serializableentity personsentity = new serializableentity(person); putrequest.setentity(personsentity);
after getting binary rest, code should below object:
httpentity httpentity = response.getentity(); inputstream inputstream = null; try { inputstream = httpentity.getcontent(); person p = (person) serializationutils.deserialize(inputstream); system.out.println("person:" + p.getname()); } { inputstream.close(); }
this worked charm !!
Comments
Post a Comment