在java

时间:2015-06-03 09:21:13

标签: java spring jaxb marshalling

我正在实施新的客户端服务器应用。 我想问一下,通过网络发送和接收对象的最佳方式是什么?

在服务器端,我有类似的东西,但我得到空指针异常:

@RequestMapping(value = "/test")
@ResponseBody
public Student myAction(){
    Student student = new Student(1,"Miso");
    return student;
}

在客户端:

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;
    }

来自客户端的堆栈跟踪:

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)

班级学生

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();
    }
}

过去我使用过JAXB Marshaller的XML消息......

有没有更新的解决方案?或者我必须使用XML?

感谢答案。

0 个答案:

没有答案
相关问题