JAX-RS发送序列化对象

时间:2014-05-02 06:18:02

标签: java rest java-ee jax-rs

作为旨在发送xml或JSON类型数据的REST服务,

@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})  

This搜索结果     将自定义Object详细说明为JSON然后进行转换。没有这样做,

任何人都可以告诉我,有没有办法直接发送序列化对象?任何资源,代码剪辑在哪里国内JAX-RS如何发送序列化对象?

2 个答案:

答案 0 :(得分:2)

是的,这确实有效。我使用相同的方式为我的Android应用程序。您可以使用Object-Input / Output-Streams。

不幸的是,我无法提供任何代码atm,因为我在工作,代码在我的家用电脑上;) 我稍后会更新这篇文章并为您提供一个示例=)

所以最后我找到了一些时间:

这是服务器端的代码。它接收一个登录字符串,并返回一个布尔值和一个字符串:

@POST
@Path("/login/{id}")
@Consumes("application/xml")
public StreamingOutput login(@PathParam("id") int id, InputStream is) {
    String login[] = null;
    ObjectInputStream ois = null;
    try {
        ois = new ObjectInputStream(is);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        login = (String[]) ois.readObject();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    this.login[0] = login[0];
    this.login[1] = login[1];
    return new StreamingOutput() {
        public void write(OutputStream outputStream) throws IOException,
                WebApplicationException {
            login(outputStream);
        }
    };
}
public void login(OutputStream os) {
    boolean result = false;
    connect();
    ResultSet rs = null;
    try {
        PreparedStatement ps = dbconn
                .prepareStatement("Select password from supervisor where username = '"
                        + login[0] + "'");
        rs = ps.executeQuery();
        rs.next();
        String password = rs.getString("password");
        login[0] = password;
        if (login[1].equals(password)) {
            result = true;
        }
    } catch (SQLException e) {
        login[0] = e.toString();
    }
    try {
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(result);
        oos.writeObject(login);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

现在这是我为设备编写的代码:

ObjectOutputStream oos = null;
    String[] login = { "xxxxxxxx", "xxxxxxxx" };
    URL url = new URL(
            "http://xxxxxxx.xxxxxxxxx.xxxxxxxxxxx.com/xxxxx/login/1");
    try {
        // creates a HTTP connection
        HttpURLConnection httpConn = (HttpURLConnection) url
                .openConnection();
        // httpConn.setUseCaches(false);
        httpConn.setReadTimeout(10000 /* milliseconds */);
        httpConn.setConnectTimeout(15000 /* milliseconds */);
        httpConn.setDoOutput(true);
        httpConn.setDoInput(true);
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Content-Type", "application/xml");
        httpConn.connect();
        OutputStream outputStream = httpConn.getOutputStream();
        oos = new ObjectOutputStream(outputStream);
        oos.writeObject(login);
        outputStream.close();
        InputStream is = httpConn.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        try {
            boolean check = (boolean) ois.readObject();
            String[] logback = (String[]) ois.readObject();
            System.out.println(check + " " + logback[0] + " " + logback[1]);
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println();
    } finally {
        if (oos != null) {
            oos.close();
        }
    }

现在这看起来有点复杂,但这是从较长的项目环境中取出的。我希望它能帮助你实现你想要的目标!

因为那么晚才再次生日。

答案 1 :(得分:0)

嘿,看看这里的java文档,我想这可能是你在找什么? http://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html