Question

As REST services intended to send xml or JSON type data,

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

and This Search Results elaborates custom Object into JSON and then, tranffer it. Without doing so,

can any one let me know, Is there a way to send serialized objects straightforward ? Any resource, code snip where domestrate how JAX-RS send serialized Objects ?

Was it helpful?

Solution

Yes, this does work. I used the same way for an android application of mine. You can just use Object-Input/Output-Streams.

Unfortunately, i cannot provide any code atm, because I am at work, and the code is on my home pc ;) I will update this post later on and provide you an example =)

So finally I found some time for this:

This is the code on the server side. It receives a login String, and returns a boolean value and a String:

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

Now this is the code I wrote for my device:

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

Now this all looks kinda complicated, but this is taken out of a longer project context. I hope it still helps you to achieve what you want!

Sry again for being that late.

ZerO

OTHER TIPS

Hey check out the java documentation here I think this may be what you are looking for? http://docs.oracle.com/javase/tutorial/essential/io/objectstreams.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top