I am implementing new client server app. I would like to ask, What is the best way to send and receive objects via network?
On server side, I have something like this but I am getting null pointer exception:
@RequestMapping(value = "/test")
@ResponseBody
public Student myAction(){
Student student = new Student(1,"Miso");
return student;
}
On client side:
public Student test(){
String prodGroup = "";
Student obj = null;
try {
url = new URL("http://localhost:8080/getData/test");
urlConn = (HttpURLConnection) url.openConnection();
InputStream stream = urlConn.getInputStream();
ObjectInputStream ois = new ObjectInputStream(stream);
obj = (Student) ois .readObject();
}
catch (MalformedURLException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (IOException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (ClassNotFoundException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
return obj;
}
Stack trace fromclient side:
java.io.IOException: Server returned HTTP response code: 406 for URL: http://localhost:8080/getData/test
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1838)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at com.leoni.ServerConnector.test(ServerConnector.java:45)
at com.leoni.Client.main(Client.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Exception in thread "main" java.lang.NullPointerException
at com.leoni.Client.main(Client.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Class Student:
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 5950169519310163575L;
private int id;
private String name;
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Student student = (Student) o;
if (id != student.id) return false;
if (name != null ? !name.equals(student.name) : student.name != null) return false;
return true;
}
public int hashCode() {
return id;
}
public String toString() {
return "Id = " + getId() + " ; Name = " + getName();
}
}
In past i have used XML messages with JAXB Marshaller...
Is there any kind of newer solution? or I have to use XML?
Thx for answers.