【问题标题】:JAX-RS send serialized Objects [closed]JAX-RS 发送序列化对象 [关闭]
【发布时间】:2014-05-02 06:18:02
【问题描述】:

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

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

This 搜索结果 将自定义对象详细说明为 JSON,然后将其传输。不这样做,

谁能告诉我,有没有办法直接发送序列化对象?任何资源,代码片段都可以说明 JAX-RS 如何发送序列化对象?

【问题讨论】:

  • 序列化是什么意思?你的意思是像二进制格式的Java序列化协议吗?
  • 您是否尝试过搜索?看看mkyong.com/java/…
  • @IwishIcouldthinkofagood。由于没有找到我搜索的内容,我在这里发布了一个问题。感谢您的参考链接。它是关于将对象转换为 json 的。在这里,我不需要将我的对象转换为任何 json 或 xml。直接发送序列化的对象?
  • 所以您想将您的对象从一个 java 应用程序序列化到另一个?
  • @Débora 那么为什么不使用 Java 序列化来序列化您的对象并回复application/octet-stream?这真的很简单。

标签: java rest jakarta-ee jax-rs


【解决方案1】:

是的,这确实有效。我对我的一个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();
        }
    }

现在这一切看起来有点复杂,但这是从更长的项目背景中取出的。我希望它仍然可以帮助您实现您想要的!

再次为来晚了感到抱歉。

【讨论】:

  • 谢谢零。如果它支持android,那么它也应该支持这个jee应用程序。当你有空时,请让我知道你在哪里获得参考或任何来源。 :)
  • 你能给我代码片段或任何证明对象类型数据在 JAX-RS 服务器应用程序和客户端之间传输的源吗?提前致谢。
  • 天哪,对不起:/我忘了这个。我今天会很晚才回家,但我会更新这篇文章!
  • 没关系零.. 我在等.. :) 。但是如果 JEE 规范上有一些东西会更好:)。
  • 嗨,零,当你有空时,请让我有工作示例代码。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2014-10-24
    • 2011-07-30
    • 2015-05-25
    • 1970-01-01
    • 2017-05-26
    • 2015-02-16
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    相关资源
    最近更新 更多