Reputation: 358
can i pass java POJO class object to client side .
for example user send request to server "/user". server should send response as User.java object
User.java class is
public class User {
private String name = null;
private String education = null;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
}
Upvotes: 0
Views: 1302
Reputation: 2052
I had the same requirement, I used servlet to do that. If you can use servlet, you can take this reference.
Servlet side
ObjectInputStream objectStream = new ObjectInputStream(request.getInputStream());
User user = (User) objectStream.readObject();
objectStream .close();
Client side
User user = new User();
urlConnectionToTarget.setRequestMethod("POST");
urlConnectionToTarget.setDoOutput(true);
urlConnectionToTarget.setDoInput(true);
urlConnectionToTarget.setRequestProperty("Content-Type", "application/octet-stream");
urlConnectionToTarget.connect();
ObjectOutputStream servletObjectStream = new ObjectOutputStream(urlConnectionToTarget.getOutputStream());
servletObjectStream.writeObject(user);
servletObjectStream.flush();
servletObjectStream.close();
Upvotes: 1
Reputation: 408
There are two simple ways that I can think of:
Either ways the XML/JSON will directly map to your Objects and are fairly easy to implement.
There are other ways but I assume that you are using a web service and these are best suited for it.
Upvotes: 1
Reputation: 11
There are many ways:
You can use any of the JSON,XML,SOAP formats.
Or you can use applet
Rmi if using standalone java client.
Upvotes: 1
Reputation: 2065
As mr.icetea suggested you can serialize the java object to json and then pass it . You can do the serialization/deserialization using the jackson library : http://www.mkyong.com/java/how-to-convert-java-object-to-from-json-jackson/
Upvotes: 1