2

I am new to Jersey. I have 2 things to know. 1. Send domain objects from client to server 2. Send domain Objects to client from server.

  1. I want to send my custom Objects from client to server application. Since types of objects that are to be sent can be differ (it can be a domain object, File or image ), I supposed to convert those objects to stream and send to server. Also with the stream, I need to send send some parameters as well. Then I need to retrieve the stream in server and process it.

  2. Once domain objects convert into stream, then they should be sent to the client as well.

    I use Jersey 2.8 . Java 8 . Tomcate 6 . Here is how I tried to do it, but it fails(Might be a wrong approach)

Here is my client:

InputStream returnStrem = (InputStream)client.target("http://localhost:8080/TestJerseyProject")
                                                                .path("rest/save")
                                                                .request(new MediaType[]    {MediaType.APPLICATION_OCTET_STREAM_TYPE})
                                                                .get(InputStream.class);

                        try {
                          ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(returnStrem));
                          Object o=  ois.readObject();
                          System.out.println("Entity  : "+o );
                      }catch(Exception e){
                    e.printStackTrace();  
                      }finally {
                     returnStrem.close();
                      }

The server side code :

@Path("/cache")
public class ObjectCacheAction {

        @GET
        @Consumes("application/octet-stream")
        @Produces(MediaType.APPLICATION_OCTET_STREAM)
        public Response streamExample(InputStream is) {

                 try {
                  ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(is));
                  Object o=  ois.readObject();
                  System.out.println("Entity : "+o );
                  is.close();
              }catch(Exception e){
             e.printStackTrace();
              }finally {

              }


          StreamingOutput stream = new StreamingOutput() {
                public void write(OutputStream os) throws IOException,
                                WebApplicationException {

                        try {
                                MyObj m=new MyObj();//My Domain Object
                                m.setName("sdfsdf");

                                 ObjectOutputStream oos1 = new ObjectOutputStream(os);
                                 oos1.writeObject(m);
                                 oos1.flush();
                                 oos1.close();

                        } catch (Exception e) {
                                e.printStackTrace();
                        }
                }
        };

          return Response.ok(stream).build();
        }

}

May be my approach is wrong. How ever, please let me know how I can I do this with working code sample. I tried over Internet, but most of them are Jersey 1.X.

Débora
  • 5,816
  • 28
  • 99
  • 171
  • Have you considered `JSON`? I can see why you wouldn't want to use JSON if your object were large but you have to serialize them somehow so the overhead might not be that bad. You get the benefit of portability and built-in marshalling from Jersey to simply things. – Baldy May 16 '14 at 18:55
  • @Baldy. Thanks. Why JSON is not considered is, the type of the data to be sent, cannot be defined. It can be any like Image,File what ever... . – Débora May 17 '14 at 02:43

2 Answers2

2
InputStream returnStrem = (InputStream)client.target("http://localhost:8080/TestJerseyProject")
            .path("rest/save")
            .request(new MediaType[] {MediaType.APPLICATION_OCTET_STREAM_TYPE})
            .get(InputStream.class);

With the last statement you are actually asking to collect an instance of InputStream where, in fact, you should be asking for the object you are expecting to receive. From the Server code it seems that you should be requesting a MyObj.class so the client code would actually be more like

MyObj myObj = client.target("http://localhost:8080/TestJerseyProject")
            .path("rest/save")
            .request(new MediaType[] {MediaType.APPLICATION_OCTET_STREAM_TYPE})
            .get(MyObj.class);

I am unfamiliar with using streams in this fashion so you may have to tweak it...

TecOpen
  • 21
  • 3
  • Thanks for your answer. Thing is, Always the returning Object is not type of MyObj. It can be any other domain object or java Object type. That is why I used the InputStream to get the byte array to recreate the final correct object based on other logics. – Débora May 16 '14 at 17:41
  • How big is the object and why is it streamed? – TecOpen May 16 '14 at 17:43
  • Size of the Object is also cannot be pre determined. The size can be 1kb to 1MB some times. Actually I/O streams are used to transfer my domain objects. Sending domain objects (like POJO) one server to another server is the goal of using this application. – Débora May 16 '14 at 17:46
  • I am recovering nearly 200 distinct Classes without streams, usually small objects but some may be 200KB. Using REST I encode my request in the url and so know what I am asking for. ej: http://localhost/test/rest/save/myobj – TecOpen May 16 '14 at 17:52
  • I am new to Jersey. You mean any object types ? even Collection classes can be sent.? What you meant is like as mentioned here: http://stackoverflow.com/questions/17541223/setting-content-type-encoding-in-jersey-rest-client – Débora May 16 '14 at 18:02
  • I am transfering XML but thats just information in the header. Jersey Marshalls/Unmarshalls the objects magically, just like serialization. You __could__ inherit the transfered objects from a common object and let deserialization do its job (hence Object.class). The example is a post which, in my case, is files, so I use a Jersey Form object with the multipart library. – TecOpen May 17 '14 at 07:11
  • 1
    have you seen this? http://stackoverflow.com/questions/12012724/jersey-example-of-using-streamingoutput-as-response-entity – TecOpen May 17 '14 at 07:19
  • Thanks for link. I already done sending stream from server to the client. But I need to know how to do in client side to send byte stream to server. – Débora May 17 '14 at 08:40
2

This Question and its answers solved my problem. In the answers, InputStreams were used. They worked well, but I had to do some modifications to make them work for Jersey 2.8. Also, Instead of stream, direct byte [] also can be used. I tested and it worked well. Big thank to Martin Wilson and sikrip .

Community
  • 1
  • 1
Débora
  • 5,816
  • 28
  • 99
  • 171